Solved

Regular expression problem for replace function of php

Posted on 2007-11-29
11
373 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
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.
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 …

947 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

22 Experts available now in Live!

Get 1:1 Help Now