Regular Expression is not working properly

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
telic
Forum Newbie
Posts: 1
Joined: Tue May 24, 2005 8:47 am

Regular Expression is not working properly

Post by telic »

Hi,

I am doing an admin panel for flash using PHP and XML, here I am using regular expression to find and replace text in the xml file.

This is the code I am using the find and replace operation

$string = "<page><![CDATA[".$text."]]></page>";
$pattern = "/<page>(.)*<\/page>/";
$contents = preg_replace($pattern,$string,$contents);


here “$text” content from textarea

if I am giving small text content it’s working fine, if I am giving four or more lines then this regular expression is not working properly.

please help me to rectify this issue....

Thanks in advance.
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

Try changing pattern to :
$pattern = "/<page>(.*?)<\/page>/is";

Are you sure you are using preg_replace correctly ? Your replacement argument seems to be the subject.
mixed preg_replace ( mixed pattern, mixed replacement, mixed subject [, int limit] )
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

:arrow: Moved to Regex :?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Yeah anjanesh is right. You need the "s" modifier because . (dot) will not match multiple lines by itself :wink:
Post Reply