Hi,
i need help on the following by using preg_replace.
Here is the code:-

//The input string

$string=<img src="images/pirzada.jpg" alt="" border="0" align="left">
<a href="news/eng6.html"><strong>
Pirzada escapes beating from lawyers
</strong>

//$what pattern should i use below??
$pattern=


//Replace the input string with the following
$replace=<<<BEGIN
<img src="images/chinto.jpg" alt="" border="0" align="left">
<a href="news/eng6.html"><strong>
News Heading will display here
</strong>
BEGIN;


$string=preg_replace($pattern, $replace, $string);

i want to know that $pattern i use so that i can get the desired results.
quick reply is needπŸ™‚

    Hi. I'm not too good with regex, and I'm not sure this is what you're looking for, but I gave it a try:

    $string = '<img src="images/pirzada.jpg" alt="" border="0" align="left">
    <a href="news/eng6.html"><strong>Pirzada escapes beating from lawyers</strong>
    ';
    
    $result = preg_replace ("/<strong>(.*)<\/strong>/", 
    "<strong>News Heading will display here</strong>", 
    $string);
    
    echo $result;
    
    

      Be careful with (.) as this can be very inefficient. To make along story short, it matches everything it can get it's hands on (up to a newline, if not more depending on modifiers used), then has to start backtracking, relinquishing one character at a time and check to see if those characters match what follows (.) in the pattern.

      Using negative look ahead assertions or negated character classes would be wiser I would think.

      EDIT: Come to think of it, I would simply use negative look ahead assertions for the sake of possible sub nested tags within <strong></strong>.

        Based on what you have done, here would be one alternative:

        $string = '<img src="images/pirzada.jpg" alt="" border="0" align="left"> 
        <a href="news/eng6.html"><strong>Pirzada <em>escapes</em> beating from lawyers</strong> 
        ';
        
        $result = preg_replace ("#<strong>(?:(?!</strong>).)+</strong>#", 
        "<strong>News Heading will display here</strong>", 
        $string);
        
        echo $result;
        

        Using negative look ahead assertions (?!), you can tell regex, as long as it is not </strong>, match it... notice how I even threw in some extra thml tags within <strong></strong> ( the <em> tags). Because of the negative look ahead assertion, we can match extra stuff inside the strong tags (this is not to suggest that using . and then backtracking won't manage the same results.. it will.. just that unless used carefully, stuff like . or .+ can be less efficient, depending on its placement within the pattern, as well as how much data the regex has to go through).

          Hi,
          thanx for all of you ppl who replied to this post.
          Acutally i want to change the value of pic. and headiing dynamically (values come through html form). All of you are suggesting me a way to change heading but none of you ve given me choice how to change img as well.

          $new_img=$Post['new_img'];
          $heading=$
          Post[''heading'];

          //input string
          $string=<img src="images/pirzada.jpg" alt="" border="0" align="left">
          <a href="news/eng6.html"><strong>
          Pirzada escapes beating from lawyers
          </strong>

          $replace=<<<BEGIN
          <img src="images/$new_img" alt="" border="0" align="left">
          <a href="news/eng6.html"><strong>
          $hading
          </strong>
          BEGIN;

          //pattern is needed
          $pattern=''

          and all i want is
          preg_replace($pattern, $replace, $string);

          Hope someone will give me the right choice, so that i can change both img. and heading.

            This is not the most efficient way, but at this hour and being as tired as I am, this is what I did:

            $new_img = 'new_image.jpg';
            $heading = 'News Heading will display here';
            
            $string = '<img src="images/pirzada.jpg" alt="" border="0" align="left">
            <a href="news/eng6.html"><strong>
            Pirzada escapes beating from lawyers
            </strong>';
            
            $result = preg_replace('#(<img.+?)(?:[^/]+\.[^"]+)(.+)#', "$1" . $new_img . "$2", $string);
            $result = preg_replace('#<strong>(?:(?!</strong>).)+</strong>#s', '<strong>' . $heading . '</strong>', $result);
            echo $result;
            

            Output (viewed as right-clicking in the browser and viewing source):

            <img src="images/new_image.jpg" alt="" border="0" align="left"> 
            <a href="news/eng6.html"><strong>News Heading will display here</strong> 
            

            Note that since I have no idea what your value of $new_img and $heading is, I used some simple values.. you would use what you currently have instead.

              I think I would replace the first preg line I did with this instead.. It's a little more concise:

              $result = preg_replace('#(<img.+?)(?:[^/]+\.[^"]+)([^>]+)#', "$1" . $new_img . "$2", $string);
              

                thank you nrg_alpha.
                It is working but it is creating problem as well.
                Because i have 30 other news on the same page.
                What this code is doing is replacing all the images with this single one and replacing all the headline with this sinlle headline news. πŸ™
                Actually i want to target a single news heading and its respective image.
                here is code again

                //these value will come from a html form
                $new_img=$Post['new_img'];
                $heading=$
                Post[''heading'];
                $news_no=$_Post[''news_no'];

                suppose i want to change news number 25 then

                $new_img='news_25.jpg';
                $heading='News no. 25 heading';
                $news_no='eng25.html';

                //input target string
                $string=<img src="images/pirzada.jpg" alt="" border="0" align="left">
                <a href="news/eng25.html"><strong>
                i am news 25 headline
                </strong>

                $replace=<<<BEGIN
                <img src="images/$new_img" alt="" border="0" align="left">
                <a href="news/$news_no"><strong>
                $hading
                </strong>
                BEGIN;

                //pattern is needed
                $pattern=

                preg_replace($pattern, $replace, $string);

                  chinto09;10890522 wrote:

                  thank you nrg_alpha.
                  It is working but it is creating problem as well.
                  Because i have 30 other news on the same page.
                  What this code is doing is replacing all the images with this single one and replacing all the headline with this sinlle headline news. πŸ™
                  Actually i want to target a single news heading and its respective image.
                  here is code again

                  //these value will come from a html form
                  $new_img=$Post['new_img'];
                  $heading=$
                  Post[''heading'];
                  $news_no=$_Post[''news_no'];

                  suppose i want to change news number 25 then

                  $new_img='news_25.jpg';
                  $heading='News no. 25 heading';
                  $news_no='eng25.html';

                  //input target string
                  $string=<img src="images/pirzada.jpg" alt="" border="0" align="left">
                  <a href="news/eng25.html"><strong>
                  i am news 25 headline
                  </strong>

                  $replace=<<<BEGIN
                  <img src="images/$new_img" alt="" border="0" align="left">
                  <a href="news/$news_no"><strong>
                  $hading
                  </strong>
                  BEGIN;

                  //pattern is needed
                  $pattern=

                  preg_replace($pattern, $replace, $string);

                  I won't have enough time to help out on the rest... I did want to make a note on your coding though..

                  By example, you have two single quotes on one side in your $POST variable?
                  $news_no=$
                  Post[''news_no'];

                  Be consistent and use sinlge quotes:
                  $news_no=$_Post['news_no'];

                  Your $string is not surrounded by single quotes:
                  $string=<img src="images/pirzada.jpg" alt="" border="0" align="left">
                  <a href="news/eng25.html"><strong>
                  i am news 25 headline
                  </strong>

                  Should be:
                  $string='<img src="images/pirzada.jpg" alt="" border="0" align="left">
                  <a href="news/eng25.html"><strong>
                  i am news 25 headline
                  </strong>';

                  I'm not sure why in your replace, you are using the heredoc (<<<BEGIN and BEGINπŸ˜‰.

                  And now you are adding a new part to the problem:
                  <a href="news/$news_no"><strong>

                  You should list everything you need upfront.. this would have saved some time trouble shooting this thing.

                  As far as all images and headers being replaced by the same one, you will need to make your $new_img, $heading and $news_no variables as arrays instead of being single valued variables.

                    DONE IT
                    Finally i have done it.
                    thanks for everyone for their helpπŸ™‚

                      Write a Reply...