Solved

REGEX: Find links without http:// and replace

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

Featured Post

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…

730 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