Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-29-2011, 07:35 AM   PM User | #1
tommy082
New to the CF scene

 
Join Date: Mar 2011
Posts: 3
Thanks: 1
Thanked 0 Times in 0 Posts
tommy082 is an unknown quantity at this point
Regular expression, help needed

Hello, I am quite new in php and I have just started using regular expressions. Could anyone help me with this expression? I don't know why it is not working.

$string = My name is [[Tom]] and I live in [[Germany|Czech republic]];
$pattern = /\[\[(.*)[^(\]\])]\|/U

$string = preg_replace("$pattern", "", $string);
$string = preg_replace("/\]/", "", $string);

echo $string;

I would expect the result to be:
"My name is Tom and I live in Czech republic"
but what I get is "My name is Czech republic".
The whole part between the first [[ and the sign | is replaced..

I have been fighting with this for hours and didn't find a solution


Thanks for any help
tommy082 is offline   Reply With Quote
Old 03-29-2011, 07:43 AM   PM User | #2
oesxyl
Master Coder


 
Join Date: Dec 2007
Posts: 6,682
Thanks: 436
Thanked 890 Times in 879 Posts
oesxyl is a jewel in the roughoesxyl is a jewel in the roughoesxyl is a jewel in the rough
i didn't test this but you can try:
PHP Code:
$string My name is [[Tom]] and I live in [[Germany|Czech republic]];
$pattern1 "/\[\[[^|]+|/"// this will replace [[Germany|
$pattern2 "/[\]\[]/"// this will replace [ and ]

$string preg_replace($pattern1""$string);
$string preg_replace($pattern2""$string);

echo 
$string
best regards
oesxyl is offline   Reply With Quote
Old 03-29-2011, 10:03 AM   PM User | #3
tommy082
New to the CF scene

 
Join Date: Mar 2011
Posts: 3
Thanks: 1
Thanked 0 Times in 0 Posts
tommy082 is an unknown quantity at this point
Unhappy

Thanks for your suggestion but unfortunatelly, this solution didn't work.
tommy082 is offline   Reply With Quote
Old 03-29-2011, 10:23 AM   PM User | #4
bullant
Banned

 
Join Date: Feb 2011
Posts: 2,699
Thanks: 13
Thanked 395 Times in 395 Posts
bullant is on a distinguished road
PHP Code:
<?php

$string 
'My name is [[Tom]] and I live in [[Germany|Czech republic]]';
$pattern1 "/(\[\[)[a-zA-Z]*\|/";
$pattern2 "/[(\[\[)(\]\])]?/";
$string preg_replace($pattern1""$string);
$string preg_replace($pattern2""$string);


echo 
$string;
?>
outputs

My name is Tom and I live in Czech republic
bullant is offline   Reply With Quote
Users who have thanked bullant for this post:
tommy082 (03-29-2011)
Old 03-29-2011, 06:29 PM   PM User | #5
tommy082
New to the CF scene

 
Join Date: Mar 2011
Posts: 3
Thanks: 1
Thanked 0 Times in 0 Posts
tommy082 is an unknown quantity at this point
Thanks a lot, this works perfectly.
tommy082 is offline   Reply With Quote
Old 03-29-2011, 11:16 PM   PM User | #6
bullant
Banned

 
Join Date: Feb 2011
Posts: 2,699
Thanks: 13
Thanked 395 Times in 395 Posts
bullant is on a distinguished road
no problem
bullant is offline   Reply With Quote
Reply

Bookmarks

Tags
expression, pattern, regex, regular

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:26 AM.


Advertisement
Log in to turn off these ads.