0

hi mate. i have problem about REGEX
this is my simple code

<?php
$teks="hi whats up? how are you?";
$replace=preg_replace("/(you)/i",'<span style="background:yellow;">\1</span>',$teks);
echo "$replace";
?>

what the mean \1 ? its not give effect for my text..
if i change the value to \2, the text has change..

thanks mate!

4
Contributors
3
Replies
4
Views
5 Years
Discussion Span
Last Post by diafol
0

\1 or $1 is the first pattern that gets match ex

$pattern = "~(\<a)(.*)(/a\>)~i";

//$1 will equal <a 
//$2 = everything after <a and before /a> etc...

a good way to know what values gets passed to $n is to do a preg_match($pattern, $str, $match) and do a print_r($match); which should output an array of what get's match.

0

If your question is not solved,then Use this!!

$replace=preg_replace('/(you)/i', '<span style="background:yellow;">\\1</span>',$teks);

Edited by samueal: n/a

0
<?php

$teks="hi whats up? how are you?";
$replace=preg_replace("/(you)/i",'<span style="background:yellow;">\1</span>',$teks);
echo $replace;

?>

works for me

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.