Solved

preg_replace question

Posted on 2008-09-30
3
265 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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 …

911 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

19 Experts available now in Live!

Get 1:1 Help Now