Skip to content

Instantly share code, notes, and snippets.

@asika32764
Forked from eduardomello/preg_replace_test.php
Created June 29, 2022 19:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save asika32764/ce78f5925332652d0c897973ed9f2503 to your computer and use it in GitHub Desktop.
Save asika32764/ce78f5925332652d0c897973ed9f2503 to your computer and use it in GitHub Desktop.
use of PHP preg_replace() to add basepath to src and href
<?php
/*
* Script that shows how to insert a basepath on HTML tags
* It looks for link, script and img tags that do not contain the
* determined basepath or a external URL
*/
// BasePath to insert in string when necessarry
$basePath = "/project/site/";
$escapedBasePath = str_replace("/", "\/", $basePath);
// regex that look for
// 0 - link tag with href without http or the basepath
// 1 - script tag with src without http or the basepath
// 2 - img tag with src without http or the basepath
$regex[0] = '/(link)(.*)(href=")(?!http)(?!' . $escapedBasePath .')/';
$regex[1] = '/(script)(.*)(src=")(?!http)(?!' . $escapedBasePath .')/';
$regex[2] = '/(img)(.*)(src=")(?!http)(?!' . $escapedBasePath .')/';
// Test strings
$str[0] = '<a href="http://somedomain.com">Test</a>'; //OK
$str[1] = '<a href="somepage/">Some Page</a>'; //OK
$str[2] = '<link rel="stylesheet" media="all" href="http://somedomain.com/style.css" type="text/css" />'; // OK
$str[3] = '<link rel="stylesheet" href="http://somedomain.com/style.css" type="text/css" media="all"/>"'; // OK
$str[4] = '<link rel="stylesheet" media="all" href="/project/site/View/CSS/style.css" type="text/css" />"'; //OK
$str[5] = '<link rel="stylesheet" href="/project/site/View/CSS/style.css" type="text/css" media="all"/>"'; //OK
$str[6] = '<link rel="stylesheet" media="all" href="View/CSS/style.css" type="text/css" />'; //NOK
$str[7] = '<link rel="stylesheet" href="View/CSS/style.css" type="text/css" media="all"/>"'; //NOK
$str[8] = '<script type="text/javascript" src="http://www.google.com/script.js"></script>'; //OK
$str[9] = '<script src="http://www.google.com/script.js" type="text/javascript"></script>'; //OK
$str[10] = '<script type="text/javascript" src="/project/site/JS/script.js"></script>'; //OK
$str[11] = '<script src="/project/site/JS/script.js" type="text/javascript"></script>'; //OK
$str[12] = '<script type="text/javascript" src="JS/script.js"></script>'; //NOK
$str[13] = '<script src="JS/sitescript.js" type="text/javascript"></script>'; //NOK
$str[14] = '<img alt="some text" src="http://www.google.com/script.js">'; //OK
$str[15] = '<img src="http://www.google.com/img.js" alt="some text">'; //OK
$str[16] = '<img alt="some text" src="/project/site/JS/img.js">'; //OK
$str[17] = '<img src="/project/site/JS/img.js" alt="some text">'; //OK
$str[18] = '<img alt="some text" src="JS/img.js">'; //NOK
$str[19] = '<img src="JS/sitescript.js" alt="some text">'; //NOK
// testing
foreach($str as $i => $line){
echo '<p><h2>TEST ' . $i . '</h2><br />';
echo 'STR: ' . htmlspecialchars($line) . '<br />';
foreach($regex as $r) {
echo 'REGEX: ' . $r;
echo '<br> <b>' . preg_match($r, $line) . '</b><br>';
}
echo '</p>';
}
// replacing
foreach ($str as $i => $line){
$buffer = $line;
$buffer = preg_replace($regex[0], '$1 $2 href="' . $basePath . '', $buffer);
$buffer = preg_replace($regex[1], '$1 $2 src="' . $basePath . '', $buffer);
$buffer = preg_replace($regex[2], '$1 $2 src="' . $basePath . '', $buffer);
echo "<p> STR " . $i . " <br /> ".htmlspecialchars($buffer)."</p>";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment