Solved

Regular expression problem for replace function of php

Posted on 2007-11-29
11
381 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to use embargo Date/time in php 29 35
paypal ipn to mysql 3 35
Presenting past 6 months bug 7 25
ebay devID, appID, certID, userToken 2 24
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

815 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

8 Experts available now in Live!

Get 1:1 Help Now