1

I have the following code:

<?php
$text = 'Hey @Mathew, have u talked with @Jhon today?';
    $text = preg_replace('/(^|\s)@([A-Za-z0-9]*)/', '\1<a href="profile.php?user=\\2">@\\2</a>',$text);
?>

And my goal is notify the user was quoted. To do that, I thought: I put all the replaced strings in a array and pick only the name; using the example above, and following the thinking, I have this result:

['Mathew', 'Jhon']

So, how can I have the last result?

9
  • 1
    How does that PHP give you {'Mathew', 'Jhon'}? Commented Apr 16, 2018 at 20:24
  • 1
    See this PHP demo. Commented Apr 16, 2018 at 20:26
  • 2
    "It's an example of array..." I'm not aware of any programing language which arrays are declared with curly brackets {}, objects normally are. Did you mean brackets [] ? Commented Apr 16, 2018 at 20:27
  • 1
    @WiktorStribiżew It's what I needed. Thank's. Commented Apr 16, 2018 at 20:30
  • 1
    Not an answer, but your pattern can be improved writing @(?<!\S.) in place of (^|\s)@ Commented Apr 16, 2018 at 20:36

3 Answers 3

2

You may actually collect matches while performing a regex-based search and replace if you use preg_replace_callback:

$text = 'Hey @Mathew, have u talked with @Jhon today?';
$names = [];
$text = preg_replace_callback('/(^|\s)@([A-Za-z0-9]*)/', function($m) use (&$names) {
        $names[] = $m[2];
        return $m[1] . '<a href="profile.php?user=' . $m[2] . '">@' . $m[2] . '</a>';
    },$text);
echo "$text\n";
print_r($names);

See the PHP demo

Output:

Hey <a href="profile.php?user=Mathew">@Mathew</a>, have u talked with <a href="profile.php?user=Jhon">@Jhon</a> today?
Array
(
    [0] => Mathew
    [1] => Jhon
)

Note the array variable for the matches is passed to the anonymous function with the use (&$names) syntax. The $m is a match object containing the whole match in the first item and captures in the subsequent items.

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

Comments

1

Before replacing the text you can use preg_match to find all users in the string:

http://php.net/manual/en/function.preg-match.php

Example:

$text = 'Hey @Mathew, have u talked with @Jhon today?';

preg_match($pattern, $text, $matches);
var_dump($matches);

$text = preg_replace('/(^|\s)@([A-Za-z0-9]*)/', '\1<a href="profile.php?user=\\2">@\\2</a>',$text);

You will have to alter your pattern for this to work.

2 Comments

Notice: Undefined variable: pattern
You'll need to define the pattern yourself, I didn't include it since yours gives back a space in the matches array the preg_match gives back. Please try reading the documentation I linked instead of just copying and pasting the code
0

I guess you can use a regex like /@[a-z0-9]+/sim, i.e.:

$text = 'Hey @Mathw, have u talked with @Jhon today?';
preg_match_all('/@[a-z0-9]+/sim', $text , $handles_array, PREG_PATTERN_ORDER);
$text = preg_replace('/(@[a-z0-9]+)/sim', '<a href="profile.php?user=$1">$1</a>', $text);
print($text);
print_r($handles_array[0]);

Output:

Hey <a href="profile.php?user=@Mathw">@Mathw</a>, have u talked with <a href="profile.php?user=@Jhon">@Jhon</a> today?Array
(
    [0] => @Mathw
    [1] => @Jhon
)

Live Demo:

https://ideone.com/oU6xbb


Note:

"It's an example of array..."

I'm not aware of any programing language which arrays are declared with curly brackets {}, objects normally are. Did you mean brackets []?

1 Comment

It seems that OP needs an array of names after replacement.

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.