php and preg replace

Posted on 2011-10-14
Last Modified: 2012-06-21
Hi Experts,

Why does this add two << around the preg replace...

I just want to add strong to first para only

$text = preg_replace('<p>', '<p><strong>', $text, 1);
$text = preg_replace('</p>', '</strong></p>', $text, 1);

Question by:maccaj51
    LVL 48

    Accepted Solution

    Wrong code, You miss the regexp delimiters. Try:

    $text = preg_replace('#<p>#', '<p><strong>', $text, 1);
    $text = preg_replace('#</p>#', '</strong></p>', $text, 1);

    Open in new window

    LVL 107

    Expert Comment

    by:Ray Paseur
    Unlike ereg() that has been deprecated and will be removed, the PCRE expressions require a delimiter.  Certain pieces of information for the regex follow the trailing delimiter.  For example, you can make the expression case-insensitive by adding the letter "i" at the end, after the final delimiter.

    You may also find that certain "meta characters" need to be escaped by prepending them with a backslash.  PHP is inconsistent in its application of this rule.  Enlightening reading here:

    Author Comment

    Hi Ray,

    Thanks for the information. I would class myself a little above beginner so will have a look...

    Is there any reason why i should accept and use hernst42 answer?
    LVL 107

    Expert Comment

    by:Ray Paseur
    Sure!  Just looking at it, I am fairly sure it works.  In some cases the wickets < and > may be treated as meta-characters, but this is not one of them.

    Author Closing Comment

    Many Thanks hernst42

    & Ray!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
    Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
    The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
    The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now