Solved

Regular expression problem for replace function of php

Posted on 2007-11-29
11
366 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
Comment Utility
<?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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
That would seem to require additional code beyond the one-pass preg_replace.
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 27

Expert Comment

by:ddrudik
Comment Utility
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
Comment Utility
Thanks for the question and the points.
0
 

Author Comment

by:z_ruixiang
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
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…
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 …

743 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

21 Experts available now in Live!

Get 1:1 Help Now