?
Solved

Regular expression problem for replace function of php

Posted on 2007-11-29
11
Medium Priority
?
421 Views
Last Modified: 2012-05-05
I want to change the following content:

<img src="abc.jpg" width="800" height="800">

to

<a href="aa.html"><img src="abc.jpg" width="800" height="800"></a>

How can I do that? I know I should use php preg_replace, but I don't know how to write the regular expression.
Thanks!

0
Comment
Question by:z_ruixiang
  • 7
  • 4
11 Comments
 
LVL 27

Expert Comment

by:ddrudik
ID: 20378149
<?php
$string = <<<EOF
<img src="abc.jpg" width="800" height="800">
EOF;
$pattern = '/(<img\x20src=.*?>)/is';
$repl = '<a href="aa.html">$1</a>';
echo preg_replace($pattern, $repl, $string );
?>
0
 

Author Comment

by:z_ruixiang
ID: 20378271
Thanks a lot, ddrudik.  I'm pretty much there.
In some cases, the string is actually:

<img onclick="onelink.html" id="12345" src="abc.jpg"  width="800" height="800">

How about like this? I want to delete all other text and just get
<a href="aa.html"><img  src="abc.jpg"></a>

It'll be great if I can replace based on the width of the picture. if width>600, put width=600. otherwise, don't put width or height.

<img onclick="onelink.html" id="12345" src="abc.jpg"  width="800" height="800">
to
<a href="aa.html"><img  src="abc.jpg" width="600"></a>

<img onclick="onelink.html" id="12345" src="abc.jpg"  width="500" height="500">
to
<a href="aa.html"><img  src="abc.jpg"></a>

Thanks a lot for your quick reply!!!
0
 
LVL 27

Accepted Solution

by:
ddrudik earned 2000 total points
ID: 20378358
See if this is what you were looking for:
$pattern = '/<img[^>]+?(src="[^"]+")[^>]+?>/';
$repl = '<a href="aa.html"><img $1></a>';
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:z_ruixiang
ID: 20378398
yeah, that's exactly what I want. Thanks!

How about the other problem about the width? Do you have any idea how to do it?
I can post another question if need.
0
 
LVL 27

Expert Comment

by:ddrudik
ID: 20378440
That would seem to require additional code beyond the one-pass preg_replace.
0
 
LVL 27

Expert Comment

by:ddrudik
ID: 20378526
This can be done in three preg_replace passes:

$pattern = '/<img[^>]*?height="[6-9]\d{2,3}"[^>]*?(src="[^"]*")[^>]*?>/is';
$repl = '<a href="aa.html"><img $1 height="600"></a>';

$pattern = '/<img[^>]*?(src="[^"]*")[^>]*?(height="[6-9]\d{2,3}")[^>]*?>/is';
$ersatz = '<a href="aa.html"><img $1 height="600"></a>';

$pattern = '/(?<!<a href="aa.html">)<img[^>]*?(src="[^"]*")[^>]*?>(?!=<\/a>)/is';
$repl = '<a href="aa.html"><img $1></a>';
0
 
LVL 27

Expert Comment

by:ddrudik
ID: 20378530
Thanks for the question and the points.
0
 

Author Comment

by:z_ruixiang
ID: 20378782
Hi, thanks!
I tried it out. It seems the first two works pretty well without the third.
But if I put the third one, everytime got empty result.

0
 
LVL 27

Expert Comment

by:ddrudik
ID: 20378792
To support heights above "999" you may want to replace:
[6-9]\d{2,3}
With:
(?:[6-9]\d{2}|[1-9]\d{3})
0
 

Author Comment

by:z_ruixiang
ID: 20378820
Thanks. Now the problem is the third one:

$pattern = '/(?<!<a href="aa.html">)<img[^>]*?(src="[^"]*")[^>]*?>(?!=<\/a>)/is';
$repl = '<a href="aa.html"><img $1></a>';

doesn't work well.
It will give an empty result for any input.
0
 
LVL 27

Expert Comment

by:ddrudik
ID: 20378851
Sample text:
<img onclick="onelink.html" id="12345" src="abc.jpg"  width="800" height="800">
<img onclick="onelink.html" id="12345" height="5800" src="abc.jpg"  width="800">
<img onclick="onelink.html" id="12345" height="77" src="abc.jpg"  width="800">
<img onclick="onelink.html" id="12345" src="abc.jpg"  width="800">

Match on:
<img[^>]*?height="(?:[6-9]\d{2}|[1-9]\d{3})"[^>]*?(src="[^"]*")[^>]*?>
Replace with:
<a href="aa.html"><img $1 height="600"></a>

Result:
<img onclick="onelink.html" id="12345" src="abc.jpg" width="800" height="800">
<a href="aa.html"><img src="abc.jpg" height="600"></a>
<img onclick="onelink.html" id="12345" height="77" src="abc.jpg" width="800">
<img onclick="onelink.html" id="12345" src="abc.jpg" width="800">

Match on:
(?<!<a href="aa.html">)<img[^>]*?(src="[^"]*")[^>]*?(height="(?:[6-9]\d{2}|[1-9]\d{3})")[^>]*?>
Replace with:
<a href="aa.html"><img $1 height="600"></a>

Result:
<a href="aa.html"><img src="abc.jpg" height="600"></a>
<a href="aa.html"><img src="abc.jpg" height="600"></a>
<img onclick="onelink.html" id="12345" height="77" src="abc.jpg" width="800">
<img onclick="onelink.html" id="12345" src="abc.jpg" width="800">

Match on:
(?<!<a href="aa.html">)<img[^>]*?(src="[^"]*")[^>]*?>
Replace with:
<a href="aa.html"><img $1></a>

Result:
<a href="aa.html"><img src="abc.jpg" height="600"></a>
<a href="aa.html"><img src="abc.jpg" height="600"></a>
<a href="aa.html"><img src="abc.jpg"></a>
<a href="aa.html"><img src="abc.jpg"></a>
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses
Course of the Month14 days, 18 hours left to enroll

840 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