Tell me, please, how to https://regex101.com write a regular expression for replacing the @ sign with the text instagram.com/» .

  • one
    Use str_replace . - Wiktor Stribiżew
  • Unfortunately, I don’t say it yet. I have already chosen the book for studying regulars "Regular expressions. Basics Author: Michael Fitzgerald Released: 2015", but I need to get an expression today, so I turned to you. How to get an expression like <a [^>] +?> ([^ <] +?) <\ / A> [|] $ 1, only to replace @ with text instagram.com/. For example, there was the word @ vanechka, and it became instagram.com/vanechka - Maxim A.
  • $s = str_replace("@", "instagram/", $s); - Wiktor Stribiżew
  • And how to turn it into such a form as this expression? <div \ s + id = [\ '"] down [\'"]> (. +?) <\ / div> [|] - Maxim A.

1 answer 1

Probably so:

 $text = 'Текст от @vanechka.'; $patt = '~@([^\s.,!?-]+)~iu'; $repl = 'instagram.com/$1'; $text = preg_replace($patt, $repl, $text); echo $text; 
  • @ Maxim A. may still need to wrap the text instagram.com/$1 in html tags to get a hyperlink as a result. - Edward
  • By the way, you can do without parentheses in a regular expression, if you replace $1 with $0 in the substitution line - mymedia
  • 2
    @mymedia without brackets you get full compliance with the template, and you must discard the "@". - Edward