Solved

preg_replace question

Posted on 2008-09-30
3
271 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
ID: 22611421
Try:
$text = preg_replace('/src="([^"]+)"/i', 'src="./\1"', $text)
0
 
LVL 19

Expert Comment

by:BrianGEFF719
ID: 22611494
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
ID: 22611717
Hi hernst42,

Thank you!

Yours works well!
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

Title # Comments Views Activity
how to call a remote wordpress to use his  content in a loop 3 35
mysql query for sum() 3 28
Restricting the scope of variables to private in OOP 4 22
Ajax and PHP 4 30
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 …
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

861 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