Solved

Regular expression problem for replace function of php

Posted on 2007-11-29
11
386 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 500 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to count occurrences of each item in an array.
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.

789 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