[Solved] Regex works sometimes doesn't work sometimes

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

Moderator: General Moderators

Post Reply
User avatar
dude81
Forum Regular
Posts: 509
Joined: Mon Aug 29, 2005 6:26 am
Location: Pearls City

[Solved] Regex works sometimes doesn't work sometimes

Post 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.
Last edited by dude81 on Sat Feb 17, 2007 2:23 am, edited 2 times in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Examples of when it works and doesn't work?
User avatar
dude81
Forum Regular
Posts: 509
Joined: Mon Aug 29, 2005 6:26 am
Location: Pearls City

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

If that was the case, more than likely, all php scripts would fail.
User avatar
dude81
Forum Regular
Posts: 509
Joined: Mon Aug 29, 2005 6:26 am
Location: Pearls City

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Have you tried strip_tags() or the variant I posted (which is linked from Useful Posts)?
User avatar
dude81
Forum Regular
Posts: 509
Joined: Mon Aug 29, 2005 6:26 am
Location: Pearls City

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

How are you splitting the text?
User avatar
dude81
Forum Regular
Posts: 509
Joined: Mon Aug 29, 2005 6:26 am
Location: Pearls City

Post 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);
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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
User avatar
dude81
Forum Regular
Posts: 509
Joined: Mon Aug 29, 2005 6:26 am
Location: Pearls City

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Unless you have different tags, it works exactly as posted.
User avatar
dude81
Forum Regular
Posts: 509
Joined: Mon Aug 29, 2005 6:26 am
Location: Pearls City

Post by dude81 »

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.
User avatar
dude81
Forum Regular
Posts: 509
Joined: Mon Aug 29, 2005 6:26 am
Location: Pearls City

Post by dude81 »

Solved, $post_array needs to be catched in else loop too. It worked fine. Nothing wrong in any regular expressions
Post Reply