[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

REGEX: Find links without http:// and replace

Posted on 2007-03-19
5
Medium Priority
?
229 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
[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
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 2000 total points
ID: 18753359
$string = preg_replace('/(href=|src=)(['"])(?!http:\/\/)([^\2]*)[\2]/','$1"http://$3"',$string);
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
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 …
Suggested Courses

650 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