1

Let us say, I want to return a name:

$re = "/(name is )[a-z- ]*( )/i";
$str = "My name is John ";

preg_match($re, $str, $matches);

print_r($matches);

The result is:

Array
(
    [0] => name is John 
    [1] => name is 
    [2] =>  
)

Well, let's see this:

$string = "My name is John and I like Ice-cream";
$pattern = "My name is $1 and I like $2";

With this I get: Array

(
    [0] => name is John and I like Ice-cream 
    [1] => name is 
    [2] =>  and I like 
    [3] =>  
)

which is more or less the same string I passed.

What I am looking for is to compare and extract the variables so that I could use them as variable $1 and $2 or anything like this works.

Or maybe a method that returns an associate array or those items like:

Array("1" => "John" , "2" => "Ice-cream")

Any workaround or ideas are highly appreciated as long as they work in the way I asked above.

3
  • You didn't put a capturing group around the name. If you did your result array would provide enough information from which you could derive the array you want. Commented Nov 11, 2015 at 10:13
  • @Marty, what about the case with multiple items? Any code to share? Commented Nov 11, 2015 at 10:15
  • 1
    You could simply make more than 1 capturing group. Commented Nov 11, 2015 at 10:17

2 Answers 2

3
$str = 'My name is John and I like Ice-cream';
$re = '/My name is (\S+) and I like (\S+)/';
preg_match($re, $str, $matches);

print_r($matches);

returns

Array
(
    [0] => My name is John and I like Ice-cream
    [1] => John
    [2] => Ice-cream
)

\\S matches non-whitespace.

Change it to . to match anything, which will work as far as the other strings in the pattern ("My name is " and " I like ") are fixed.

Sign up to request clarification or add additional context in comments.

14 Comments

You don't need to escape the `` in your regex. And this will fail if a name contains whitespace or if the think you like does.
Awesome. I guess I can take the boat from here.
@arjan Added a some text concerning the whitespace issue. However, the thing about escaping I do not understand.
Umm, @Arjan, this will fail if a name contains whitespace yes that's true. But, is there a way to overcome?
@syck Concerning the whitespace, I would simply use .* or .+ to match any characters.
|
1

Not so clean but might be helpful...

$string = "My name is John Cena and I like Ice-cream";
$pattern = "My name is $ and I like $";
print_r(getArray($string, $pattern));

function getArray($input, $pattern){

    $delimiter = rand();
    while (strpos($input,$delimiter) !== false) {
        $delimiter++;
    }

    $exps = explode("$",$pattern);
    foreach($exps as $exp){
        $input = str_replace($exp,",", $input);
    }

    $responses = explode(",", $input);
    array_shift($responses);
    return $responses;

}

It returns:

Array
(
    [0] => John Cena
    [1] => Ice-cream
)

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.