Any questions involving matching text strings to patterns - the pattern is called a "regular expression."
Moderator: General Moderators
dude81
Forum Regular
Posts: 509 Joined: Mon Aug 29, 2005 6:26 am
Location: Pearls City
Post
by dude81 » Sat Feb 03, 2007 6:54 am
Hi
I wrote a regular expression for
Code: Select all
<gml:coordinates>45.67, 88.56</gml:coordinates>
Firstly, I've just started writing regular expressions. The regular expression I've written is
Code: Select all
preg_replace("/(<.?(gml.*).*>)/U", '\n',$value,-1,$x);
i.e anytime it a mail comes with these tags, it replaces all of them with new line. this works sometimes and doesnt work sometime.
Last edited by
dude81 on Sat Feb 17, 2007 2:23 am, edited 2 times in total.
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Sat Feb 03, 2007 8:02 am
Examples of when it works and doesn't work?
dude81
Forum Regular
Posts: 509 Joined: Mon Aug 29, 2005 6:26 am
Location: Pearls City
Post
by dude81 » Sat Feb 03, 2007 8:05 am
The situation is when I uploaded to a local server(Linux) on whom I'm the root. It worked fully well. When I opened and kept on the internet, where I'm an ftp user , it doesn't work there.
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Sat Feb 03, 2007 8:09 am
If that was the case, more than likely, all php scripts would fail.
dude81
Forum Regular
Posts: 509 Joined: Mon Aug 29, 2005 6:26 am
Location: Pearls City
Post
by dude81 » Sat Feb 03, 2007 8:16 am
You mean to say there is nothing wrong in my regular expression(I will be glad if that is the case). I'm trying to replace all the gml tags with a new line.
The output on Local server is like this
For the following mail
Code: Select all
<gml:Point gml:id="p21" srsName="urn:ogc:def:crs:EPSG:6.6:4326"> <gml:coordinates>45.67, 88.56</gml:coordinats> </gml:Point> Checking on blogapp
Output
Code: Select all
//This is the array where
Array
(
[0] => \n
[1] =>
[2] => \n
[4] => \n
[5] =>
[6] => \n
[7] => Checking on blogapp
)
On out side server for the same message
Code: Select all
Array
(
[0] =>
[1] =>
[2] =>
[4] =>
[5] =>
[6] =>
[7] =>
)
It doesn't replace with new line , it comes out simply
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Sat Feb 03, 2007 8:30 am
Have you tried
strip_tags() or the variant I posted (which is linked from Useful Posts)?
dude81
Forum Regular
Posts: 509 Joined: Mon Aug 29, 2005 6:26 am
Location: Pearls City
Post
by dude81 » Sat Feb 03, 2007 8:33 am
I don't want to try strip_tags, there can be even some of the html content in the mail.
Let me make my problem still clear.
Firstly if any mail comes with
tags I split the entire mail content. Now gml also being present in that tags, I run a loop and replace all the gml tags with the above regular expression for a newline.
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Sat Feb 03, 2007 9:21 am
How are you splitting the text?
dude81
Forum Regular
Posts: 509 Joined: Mon Aug 29, 2005 6:26 am
Location: Pearls City
Post
by dude81 » Sat Feb 03, 2007 9:32 am
Based on the following regex
Code: Select all
$pattern = '/(<(?:[^<>]+(?:"[^"]*"|\'[^\']*\')?)+>)/';
$gml_content=preg_split($pattern,trim($post_content), -1,PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Sat Feb 03, 2007 10:31 am
The following seems to work just fine
Code: Select all
<?php
$t =<<<STOP
<gml:Point gml:id="p21>" srsName="urn:ogc:def:crs:EPSG:6.6:4326">
<gml:coordinates>45.67, 88.56</gml:coordinates>
</gml:Point>
Checking on blogapp
STOP;
$s = preg_replace('#</?gml[^\s>]+(?:\s*[a-z:]+(?:\s*=\s*(?:".*?"|\'.*?\'|[^\s>]+))?)*.*?>#is', "\n", $t);
echo $t, PHP_EOL, '----------', PHP_EOL, $s;
?>outputs
Code: Select all
<gml:Point gml:id="p21>" srsName="urn:ogc:def:crs:EPSG:6.6:4326">
<gml:coordinates>45.67, 88.56</gml:coordinates>
</gml:Point>
Checking on blogapp
----------
45.67, 88.56
Checking on blogapp
dude81
Forum Regular
Posts: 509 Joined: Mon Aug 29, 2005 6:26 am
Location: Pearls City
Post
by dude81 » Sat Feb 03, 2007 10:47 am
Firstly Gml content is a variable, it varies with inside numbers. The digits are picked up in the loop and made blank.
I tried with your regular expression to replace just gml tags. It didnt work successfully. Ill try alterations on that. Thanks for your effort on that.
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Sat Feb 03, 2007 12:42 pm
Unless you have different tags, it works exactly as posted.
dude81
Forum Regular
Posts: 509 Joined: Mon Aug 29, 2005 6:26 am
Location: Pearls City
Post
by dude81 » Sat Feb 03, 2007 10:08 pm
Sorry I cannot keep this code for long. I've some restrictions Kindly do understand.
Last edited by
dude81 on Mon Mar 05, 2007 6:24 am, edited 3 times in total.
dude81
Forum Regular
Posts: 509 Joined: Mon Aug 29, 2005 6:26 am
Location: Pearls City
Post
by dude81 » Wed Feb 07, 2007 1:01 am
Solved, $post_array needs to be catched in else loop too. It worked fine. Nothing wrong in any regular expressions