Page 1 of 1

[Solved] Regex works sometimes doesn't work sometimes

Posted: Sat Feb 03, 2007 6:54 am
by dude81
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.

Posted: Sat Feb 03, 2007 8:02 am
by feyd
Examples of when it works and doesn't work?

Posted: Sat Feb 03, 2007 8:05 am
by dude81
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.

Posted: Sat Feb 03, 2007 8:09 am
by feyd
If that was the case, more than likely, all php scripts would fail.

Posted: Sat Feb 03, 2007 8:16 am
by dude81
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

Posted: Sat Feb 03, 2007 8:30 am
by feyd
Have you tried strip_tags() or the variant I posted (which is linked from Useful Posts)?

Posted: Sat Feb 03, 2007 8:33 am
by dude81
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

Code: Select all

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

Posted: Sat Feb 03, 2007 9:21 am
by feyd
How are you splitting the text?

Posted: Sat Feb 03, 2007 9:32 am
by dude81
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);

Posted: Sat Feb 03, 2007 10:31 am
by feyd
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

Posted: Sat Feb 03, 2007 10:47 am
by dude81
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.

Posted: Sat Feb 03, 2007 12:42 pm
by feyd
Unless you have different tags, it works exactly as posted.

Posted: Sat Feb 03, 2007 10:08 pm
by dude81
Sorry I cannot keep this code for long. I've some restrictions Kindly do understand.

Posted: Wed Feb 07, 2007 1:01 am
by dude81
Solved, $post_array needs to be catched in else loop too. It worked fine. Nothing wrong in any regular expressions