Solved

preg_replace question

Posted on 2008-09-30
3
262 Views
Last Modified: 2008-09-30
<img src="abc.jpg">

I want to replace abc.jpg with ./abc.jpg, so that <img src="./abc.jpg">

Notes

1) image file may start with digit, eg:123.jpg

2) src does not always follow img, eg: <img width="125" src="abc.jpg" height="456">

Any idea on how to do it?

Thanks,
0
Comment
Question by:tiger0516
3 Comments
 
LVL 48

Accepted Solution

by:
hernst42 earned 500 total points
Comment Utility
Try:
$text = preg_replace('/src="([^"]+)"/i', 'src="./\1"', $text)
0
 
LVL 19

Expert Comment

by:BrianGEFF719
Comment Utility
hrenst42's suggestion is exactly what you've requested, but I'll point out additional things:

 1) If you only want to match images that don't start with ./ already then you need to do: /src="[^./]+([^"]+)"/i
 2) You should really use $1 as it is preferred to \1.


$text = preg_replace('/src="[^./]+([^"]+)"/i', 'src="./$1"', $text)
0
 
LVL 1

Author Comment

by:tiger0516
Comment Utility
Hi hernst42,

Thank you!

Yours works well!
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

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…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
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.

762 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

10 Experts available now in Live!

Get 1:1 Help Now