Solved

REGEX: Find links without http:// and replace

Posted on 2007-03-19
5
222 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

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.  …
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
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 …

810 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