i have following in my database

<table><tr><td><img src="default.jpg" width="160" style="border:1px #87925d solid;padding:2px;"></td>
<td valign="top"><a href="mylinkhere" target="_blank"><strong>
Vicky Bhai Part 4</strong></a><br><font size="1" color="#cccccc">http://www.mysite.com</font><br>Stage Funny Stuff</td></tr></table>

if want to add a <a> anchor tag to only image before displaying it on page.................how can i do that with preg match or preg replace

Recommended Answers

All 4 Replies


The preg_replace function would be better for that. The preg_match function is used to search a string, not change it. (Not directly at least.)

You would have to create a regular expression to search for you image tag, capture the contents of it, and use that to create a new version to replace it.

For example:

header('content-type: text/plain; charset=utf8');

// Some test HTML, in which we want to put a <a> tag around the image.
$html = <<<HTML
    <img src="http://example.com/my_img.png" alt="Example Image" />
    <span>Some text accompanying the image.</span>

// The search regular expression
$regexp = '/(<img [^>]+?>)/i';

// The replacement string. The $1 represents the <img> tag found
// by the regular exression.
$replace = '<a href="http://php.net/">$1</a>';

// Do the replace and print it.
$output = preg_replace($regexp, $replace, $html);
echo $output;

This prints:

    <a href="http://php.net/"><img src="http://example.com/my_img.png" alt="Example Image" /></a>
    <span>Some text accompanying the image.</span>

To explain the regular expression: '/(<img[^>]+?>)/i' .

  1. First, the outer-most parts of the expression:

    This servers as something like the quote-marks do for strings in PHP. The / chars mark the start and end of the expression. The "i" at the end makes the expression case-insensitive.

  2. We need to search for an <img> tag, so we start by adding the start tag to the expression:

    Like this, the expression would replace the start tags of every <img> tag.

  3. Not exactly what we wanted, so we add the end tag as well.

    Now this would replace whole image tags... just as long as there is nothing inside them.

  4. To allow more than just empty tags, we have the expression match every character except the end-tag character withing the <img> tag:

    The [^>] part simply means: "Every character except the > character." The + means: "More than one", and the ? means: "Up until the next unwanted character".
    We need the ? there because by default, regular expression try to grab as much as they can, or up until the last unwanted character.

  5. And finally, we enclose the whole tag inside round brackets, ().

    This will "capture" what is inside them, allowing us to use whatever they "capture" in the replacement string. Each item that is captured can be used like a PHP variable in the replacement string. ($1, $2, etc..)

Hope that helps.

thanks boss................thanks a lot

Hi I have the same kind of problem.
And not quite sure how to fix it...

I have this as an input:

<p>Hello wo<em>rld</em></p>
<p>How are you <span style='text-decoration:underline'>doing?</span></p>

i need to have the following output:

<p><span id='w_1'>Hello</span> <span id='w_2'>wo<em>rld</em></span></p>
<p><span id='w_3'>How</span> <span id='w_4'>are</span> <span id='w_5'>you</span> <span id='w_6'><span style='text-decoration:underline'>doing?</span></span></p>

Any ideas where to start with ?
thanks a lot in advance !

It is basically adding SPAN tags around every word of a text by keeping the original HTML code intact.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.