Hi everyone,
I have a problem with my regular expression. Im not very good at regular expression. Ill explain what i want,
I have a string that contain ' ','&',' ' characters. That string can also contain '&' mark anywhere. so what i want is to replace every '&' mark with ' & ' but i dont want to convert '&' mark for above characters(' ','&',' ').
here`s what i wrote,
$filename='test.xml';
$fileContents="hello this is a &test & just to test my & regualar& expression";
$fileContents=eregi_replace('&[^nbsp;|^amp;|^#]', ' & ', $fileContents);
if(!file_exists($filename))
{
print "Error : invalid file name";
die();
}
else
{
if($fp=fopen($filename,"w+"))
{
if(!fwrite($fp,$fileContents))
{
print "Error writting to the file";
}
}
else
{
print "Error opening file";
}
fclose($fp);
}
print "ok";
Out put
hello this is a & est & just to test my & regualar & expression
but if you carefully check the output u`ll see, the '&' in the word 'test' was replaced with ' & ' , but it replaced the letter 't' also.
I cannt figure out why 😕
Really appreciate all your help..
Thanx in advance
regards,
Niroshan