Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Mar 2011
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    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

  • #2
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    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

  • #3
    New to the CF scene
    Join Date
    Mar 2011
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Unhappy

    Thanks for your suggestion but unfortunatelly, this solution didn't work.

  • #4
    Banned
    Join Date
    Feb 2011
    Posts
    2,699
    Thanks
    13
    Thanked 395 Times in 395 Posts
    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

  • Users who have thanked bullant for this post:

    tommy082 (03-29-2011)

  • #5
    New to the CF scene
    Join Date
    Mar 2011
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks a lot, this works perfectly.

  • #6
    Banned
    Join Date
    Feb 2011
    Posts
    2,699
    Thanks
    13
    Thanked 395 Times in 395 Posts
    no problem


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •