Solved

preg_replace question

Posted on 2008-09-30
3
276 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

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 i…
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…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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 …

724 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