Hi,

my problem is:

<?
$string = '<link href="css/test.css" rel="stylesheet"><script language="JavaScript" src="js/test1.js"></script><script language="JavaScript" src="js/test2.js"></script>';

$expression = '/<(.background|link.href|.src)=[\'|"]?(.?)[\'|"]?(W?| .*?)>/i';

preg_match_all( $expression, $string, $matches );

$matches[ 2 ] = array_unique( $matches[ 2 ] );

foreach ( $matches[ 2 ] as $str_value ) {
$string = str_replace( $str_value, '/home/' . $str_value, $string );
}
echo htmlentities( $string );
?>

The epression doesn't replace the second path.

WHY??? and WHAT'S THE SOLUTION?

Thanks.

    Hi aouriques,

    There might be other ways, but ...

    Use this regex in single-quotes and put backslashes in front of the single-quotes ... difficult to display below ...
    /<(\w+)[>]+(href|src)=(\'|")([\'"])(\'|")[>]>/i

    <? 
    $original_string = '<link href="css/test.css" rel="stylesheet"><script language="JavaScript" src="js/test1.js"></script><script language="JavaScript" src="js/test2.js"></script>'; 
    
    $expression = SEE ABOVE; 
    $match_info = array();
    $string = $original_string;
    while(preg_match($expression, $string, $matches)){
    	$full_match = $matches[0];
    	//echo '<hr><b>Full match</b>: <pre>'.htmlspecialchars($full_match).'</pre>'; 
    
    $match_info[] = array(
    	'tag' => $matches[1]
    ,	'name' => $matches[2]
    ,	'value' => $matches[4]
    );
    $string = preg_replace($expression, '', $string, 1);
    }
    echo '<hr><b>$match_info</b>: <pre>';	print_r($match_info);	echo '</pre>';
    ?> 

    Paul 🙂

      Good morning,

      This should also work.

      $string = '<link href="css/test.css" 
      rel="stylesheet"><script language="JavaScript" 
      src="js/test1.js"></script><script language="JavaScript" 
      src="js/test2.js"></script>'; 
      
      $pattern='#((?:href|src)=[\'"])([^\'"]*)#i';
      
      $replc="$1/home/$2";
      
      echo htmlentities(preg_replace($pattern, $replc, $string));
        Write a Reply...