0

Let's say I put the following into a textbox:

1|[Guangzhou Evergrande](//www.gzevergrandefc.com/)|6|5-1-0|+15|**16**
2|[Shandong Luneng](//www.lunengsports.com/)|7|5-1-1|+7|**16**
3|[Qingdao Jonoon](//www.zhongnengfc.com/)|7|4-3-0|+4|**15**
4|[Beijing Guoan](//www.fcguoan.com/)|7|3-3-1|+2|**12**

when I press enter, it would take what's between the [ and ] and ( and ) and put it into new lines like this:

if ($name == "NAME_HERE") $name = "[".$name."](URL_HERE)";

I tried doing preg_match and using this pattern: $pattern = '/^[/'; and $pattern_end = '/^]/'; for the name -- jsut to test -- but I cannot get it to work....

Here is what I have so far:

$string = '1|[Guangzhou Evergrande](//www.gzevergrandefc.com/)|6|5-1-0|+15|**16**';
$pattern =  '/\[(.*?\)].*?\((.*?)\)/';
$replacement = 'if ($name == "{1}") $name = "[".$name."]({2})";';
echo preg_replace($pattern, $replacement, $string);
1
  • (.*?\) needs to be (.*?) since that bracket is for the subpattern Commented Apr 29, 2013 at 21:24

2 Answers 2

2

Your pattern should be

 '/\[(.*?)\]\((.*?)\)/' 

because [ ] and ( ) are special characters. Use preg_match_all() function

when you use preg_match_all you can put () for submatches(subpattern)

example

<?php

$string = '1|[Guangzhou Evergrande](//www.gzevergrandefc.com/)|6|5-1-0|+15|**16**';
$matches = array();
preg_match_all("/(<([\w]+)[^>]*>)(.*?)(<\/\\2>)/", $string, $matches);

var_dump($matches);
?>
Sign up to request clarification or add additional context in comments.

2 Comments

I edited my question to include progress: I got Warning: preg_replace(): Compilation failed: missing ) at offset 21 ... fix.php on line 5
I got it using preg_array match and such! Tahanks.
1

Perhaps I don't fully understand what you are doing here, but, I see a data structure that can be exploded without a hassle. For example:

function get_football( $text ) {
    $r = array();
    $t = explode("\n", $text );
    foreach( $t as $l ) {
        $n = explode("|", $l);
        $r[] = $n;
    }
    return( $r );
}

This will get you a nicely structured set of data that you can foreach() through and further process. If the original text is stored inside $variable0, print_r( get_football( $variable0 ) ); will show a nice structure:

Array
(
    [0] => Array
        (
            [0] => 1
            [1] => [Guangzhou Evergrande](//www.gzevergrandefc.com/)
            [2] => 6
            [3] => 5-1-0
            [4] => +15
            [5] => **16**
        )

    [1] => Array

Of course, that $n[1] name can be broken down further in the loop. Anyhow, thereafter, you can loop through whatever menu you're building with a foreach() loop instead of hardcoding menu choices. Just something to consider.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.