A
A
Artur Yalaltdinov2016-04-03 09:50:20
PHP
Artur Yalaltdinov, 2016-04-03 09:50:20

Regular season. How to detect the presence of a certain fragment inside [] and replace it?

Hello everyone, I'm not strong in regular expressions, so the question is
in wall.get text comes with such content
[club1|ВКонтакте API]
. How can I replace such text with a link?
<a href="http://vk.com/club1">Вконтакте API</a>

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
IceJOKER, 2016-04-03
@crazy_str

/\[(.+)\|(.+)\]/
www.phpliveregex.com/p/fbQ

P
Petr Flaks, 2016-04-06
@neluzhin

The previous answer, where you were recommended a boob-like regular season, is partly correct, but only partly. The problem is that in square brackets there may not be a link to a group, public, event or person. For example, [x|test] in a social network will not be converted into a hyperlink and a plain text will be displayed, and you will create a hyperlink leading to a 404 page.
You should only process hyperlinks that start with club or id and end with a number. VKontakte does not store hyperlinks in any other format. The correct regular expression looks something like this:
And you can insert this goodness like this:
Well, a good example in PHP for the smallest:
It should be noted that public pages (public) and events (events) in the form of hyperlinks also have the club prefix , and not public and event .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question