[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 231
  • Last Modified:

REGEX: Find links without http:// and replace

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
qwertq
Asked:
qwertq
1 Solution
 
glcumminsCommented:
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
 
qwertqAuthor Commented:
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
 
steelseth12Commented:
$string = preg_replace("@(href=\"|src=\")(?!http:\/\/)@","REPLACE HERE",$string);
0
 
JamesCsslCommented:
$string = preg_replace('/(href=|src=)(['"])(?!http:\/\/)([^\2]*)[\2]/','$1"http://$2"',$string);
0
 
JamesCsslCommented:
$string = preg_replace('/(href=|src=)(['"])(?!http:\/\/)([^\2]*)[\2]/','$1"http://$3"',$string);
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now