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!

Recommended Answers

All 3 Replies

\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.

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

$replace=preg_replace('/(you)/i', '<span style="background:yellow;">\\1</span>',$teks);
Member Avatar for diafol
<?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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.