0

I have a string like this into my page php:

$string = 'this is a test for @john and I can do all @mike';

I would like to take this string find all string inside it after @ and use it to find the name of the user with that user id if exist and convert into a link to become something like this:

 $string = 'this is a test for <a href="/user?id=111">@john</a> and I can do all <a href="/user?id=112">@mike</a>';

How to take all string and use it to find the id of that user and after substitute the original string with a link?

I know that using preg_match I can take the string inside it but how to use this string? ando how construct this expression to take the name after @?

Thanks

1
  • preg_match_all to get all the @ tokens then create an array of replacements use str_replace Commented Jul 29, 2013 at 10:37

4 Answers 4

1

If you have a function, say, link(), that takes the string john and returns the appropriate link:

preg_replace_callback('/@(\w+)/', function($matches) {
    return link($matches[1]);
}, $string);

Or, for older PHP versions:

preg_replace('/@(\w+)/e', 'link(\'$1\')', $string);
Sign up to request clarification or add additional context in comments.

10 Comments

Ok but I have to find the id of the user dynamically I don't know which user I have "tagged" I want to take the string after @ make a query foreach match, retrieve its id and aftyer replace into the string
@AlessandroMinoccheri, exactly. link() would be called with john as an argument. You can use john to find out whatever you need to find out, make database queries, etc, and return link.
and in your function inside the function before return I have to make the query? I don't understand how to create a link because I have to take id from database @rid
@AlessandroMinoccheri, what do you need to take the ID from the database? I presume you need to make a query, use john in it, and that query would return the ID, correct?
yes I want to take john(and after mike) send a query with that name retrieve the id (if exist a user with that name) and create the link if is possible @rid
|
1

I would use preg_match_all The following code will extract every word from the string begining with @ and return it into an array called $matches. You can then loop through the array comparing it and conditioning to it suit your needs.

$string = 'this is a test for @john and I can do all @mike';

preg_match_all('/(?!\b)(@\w+\b)/', $string, $matches);

Comments

0
$string = 'this is a test for @john and I can do all @mike';
$result = preg_replace('/([^@]+)([^\s]+)/simx', '$1<a href="yourlink">$2</a>', $string);

echo $result;

OUTPUT

this is a test for <a href="yourlink">@john</a> and I can do all <a href="yourlink">@mike</a>

1 Comment

No, my link is to take after match because I don't know the id of the user after the @, I have first take all match user, take for every its id and after replace with a link
0

I would make use of the preg_match_all() function.

After that I would loop over the matches and retrieve their IDs, and I generate the appropriate link for each of them, storing them in an array let's call it $links.

At this point I would have two arrays:

  1. $matches - which would contain all the occurrences of my pattern
  2. $links - which would contain all the respective links in a 1-1 correspondence.

Finally I would use preg_replace() in the following way:

preg_replace($matches, $links, $initial_string);

This will replace each item in $matches that finds in the $initial_string with the corresponding item from $links.

I hope I helped you. Maybe later I will be able to provide some code as well.

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.