Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9182011
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T18:27:31+00:00 2026-06-17T18:27:31+00:00

Here is my code: echo "<br />"; preg_match_all("|<[^>]+>.*</[^>]+>|U", "<b>example:</b><strong>this is a test</strong>", $out, PREG_PATTERN_ORDER);

  • 0

Here is my code:

echo "<br />";
preg_match_all("|<[^>]+>.*</[^>]+>|U",
    "<b>example:</b><strong>this is a test</strong>",
    $out, PREG_PATTERN_ORDER);
print_r($out);
echo "<br />";

echo "<br />";
preg_match_all("|<[^>]+>.*</[^>]+>|",
    "<b>example:</b><strong>this is a test</strong>",
    $out, PREG_PATTERN_ORDER);
print_r($out);
echo "<br />";

There is something I do not understand. What difference that is make when there is a U at the end of the regex?

The output is:

Array ( [0] => Array ( [0] => example: [1] => this is a test ) )

Array ( [0] => Array ( [0] => example:this is a test ) )

So what is happening here really?
Which version is the greedy version and why?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T18:27:33+00:00Added an answer on June 17, 2026 at 6:27 pm

    The U tells your regular expression to be “Ungreedy”. Greedy means to try to match as much as possible whereas “ungreedy” only takes the smallest match.

    So in the greedy example your match is:

    <b>example:</b><strong>this is a test</strong>
    

    I assume the html tags "</b><strong>" are stripped away either when you output it or by the preg_match already.

    In contrast the ungreedy does what you want by matching like this:

    <b>example:</b>, <strong>this is a test</strong>
    

    EDIT:

    To achieve a similar match using the ? you can do:

    preg_match_all("|<[^>/]+>.*?</[^>]+>|",
        "<b>example:</b><strong>this is a test</strong>",
        $out, PREG_PATTERN_ORDER);
    print_r($out);
    

    This is because .*? will try to limit the content in between the tag to be as short as possible (ungreedy), therefore again resulting in two matches.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is the code: $('#sousmenu a').click (function (){ startSlideshow(<?php echo json_encode(glob(photos- .$_GET[folder]. /*.jpg));?>); return
Here is my code <html> <body> <?php echo <b>Hello World</b> <br />; ?> </body>
Ok the error is showing up somewhere in this here code if($error==false) { $query
Here is code example class A{ int i; public: A(int i) : i(i) {}
My query outputs this in phpMyAdmin: Code: <?php if ($stmt = $mysqli->prepare("SELECT service_names.id, service_names.name
This question is in continuation to my previous post located here . Since there
Disclaimer: I'm not a real developer, so I've probably got some code spaghetti here...
This API gives me a headache.. Really can't figure out what is wrong here..
I have a php code here and I would like to create a "back"
The script is on jsfiddle here : CODE What it does at the moment

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.