Solved

REGEX: Find links without http:// and replace

Posted on 2007-03-19
5
217 Views
Last Modified: 2008-03-06
I have a string and need to find all links which do not start with http:// before them and insert a string before them. this would be for links like:

src="http://
href="http://

0
Comment
Question by:qwertq
5 Comments
 
LVL 24

Expert Comment

by:glcummins
ID: 18752520
Is a regular expression required? Can you use:

str_replace("src=\"", "src=\"http://", $string);

You would just have to check that the 'http:// does not already exists in the string.
0
 

Author Comment

by:qwertq
ID: 18752559
that is why i was assuming a regex would be required. you have to check to make sure that http:// does not immediately follow the href=" in the string.

also you need to understand that href=" might occur numerous times within the string. it not not just one value.
0
 
LVL 20

Expert Comment

by:steelseth12
ID: 18752778
$string = preg_replace("@(href=\"|src=\")(?!http:\/\/)@","REPLACE HERE",$string);
0
 
LVL 11

Expert Comment

by:JamesCssl
ID: 18753351
$string = preg_replace('/(href=|src=)(['"])(?!http:\/\/)([^\2]*)[\2]/','$1"http://$2"',$string);
0
 
LVL 11

Accepted Solution

by:
JamesCssl earned 500 total points
ID: 18753359
$string = preg_replace('/(href=|src=)(['"])(?!http:\/\/)([^\2]*)[\2]/','$1"http://$3"',$string);
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
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 dynamically set the form action using jQuery.
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 …

757 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

18 Experts available now in Live!

Get 1:1 Help Now