Good day. The beginner in regular expressions, but I want to understand. There is a string, it needs to be reduced to one type, i.e. leave only the name. Options: http://vk.com/id62334653 vk.com/id62334653 id62334653, etc.

When working with value:

$data = 'http://vk.com/id62334653'; $pattern = '/((http(s)?:\/\/)?(new\.)?(m\.)?(vk\.com\/)?)([a-z_0-9.]*)?(.*)?/ius'; $data = preg_replace($pattern, '[$7]', trim($data)); echo '<br />'.$data2.'='.$data; 

No problem, the result: http://vk.com/id62334653=[id62334653]

But if the string:

 $data = 'ΠœΠΈΡ…Π°ΠΈΠ» УспСнский http://vk.com/m.uspenskiy'; 

result: Mikhail Uspensky [m.uspenskiy]

They advised to do this:

 $pattern = '/((http(s)?:\/\/)?(new\.)?(m\.)?(vk\.com\/)?)([a-z_0-9.]*)?(.*)?/ius'; $data = preg_replace($pattern, '[$7]', trim($data)); echo '<br />'.$data2.'='.$data; 

But the problem is that a blank line is issued here if we remove the "?" after (http (s)?: //), then everything is fine, but then expressions like "vk.com/m.uspenskiy" do not pass, because there is no "http" in it, but this input option is also possible.

A question how to consider all this data? those. How to consider the options: "vk.com/m.uspenskiy" and 'Mikhail Uspensky http://vk.com/m.uspenskiy ', so that the result is "m.uspenskiy"?

  • because your pattern coincides with vk.com/m.uspenskiy 'and $ 7 corresponds to the standard m.uspenskiy, so the url is replaced. And "Mikhail Uspensky" is not added anywhere, but simply is present in the original line - vitidev
  • To make it easier to deal with regulars, I advise regex101.com , especially there is the item "regex debugger" - shows step by step how the handler understood your pattern. - toxxxa

1 answer 1

You find in the full line ( ΠœΠΈΡ…Π°ΠΈΠ» УспСнский http://vk.com/m.uspenskiy ) a substring on the specified pattern ( http://vk.com/m.uspenskiy ) and replace it with its part ( [m.uspenskiy] ). So it turns out.

If you need to get the desired part in a single variable, you need to use something like this:

 $data = 'ΠœΠΈΡ…Π°ΠΈΠ» УспСнский http://vk.com/m.uspenskiy'; $pattern = '/((http(s)?:\/\/)(new\.)?(m\.)?(vk\.com\/)?)(?P<id>.*)?/ius'; preg_match($pattern, $data, $matches); $id = isset($matches['id']) ? $matches['id'] : ''; echo '<br />'.$data.'='.$id; 

preg_match roughly speaking, parses the input string into parts according to the pattern and you can get each found part separately.

Notice that I added ?P<id> inside the brackets in the part of the regular season I want to see in $matches['id'] . This allows you to safely change the regular schedule without being tied to the group index.

  • Thank you, but "Mikhail Uspensky vk.com/m.uspenskiy " for "$ pattern = '/ ((http (s)?: \ / \ /)? (New \.)? (M \.)? (Vk \ .com \ /)?) ([a-z_0-9.] *)? (. *)? / ius'; " does not fit, because blank line is issued. - Maksim147
  • those. not how to consider the options: "vk.com/m.uspenskiy" and 'Mikhail Uspensky vk.com/m.uspenskiy ' - Maxim147
  • @ Maxim of course you can. I offered you an option based on a specific regular season. If you change the regular season - of course, it does not fit. Read how preg_match works and understand everything. In my code I took the substring 7 found by the regular. You need to change the number. or slightly modify the regular schedule to the universal version. I can not give a specific code now, later - maybe. - Ivan Pshenitsyn
  • @ Maksim147 supplemented the answer. - Ivan Pshenitsyn
  • @ Maksim147 final answer you approached? - Ivan Pshenitsyn