Replacing a Pattern with a Found String : String Replace « String « PHP






Replacing a Pattern with a Found String

<html>
<head>
<title>preg_replace</title>
</head>
<body>
<?php
     $string = "<a href=\"http://www.java2s.com/\">java2s</a><br />";
   
     $pattern = '/<a[ .]*?(href *= *".*?").*?>(.*?)<\/a>/i';
     $replacement = '\2: <a \1>\1</a>';
   
     $string = preg_replace($pattern, $replacement, $string);
     print($string);
?>
</body>
</html>

           
       








Related examples in the same category

1.Quantifier Syntaxes
2.str_replace: @ (at)
3.Replace the tag content without getting rid of any attributes
4.String replace with Regular Expressions
5.What happens when multiple instances of the search string overlap?
6.String replace: index and value