Andre Chenier
Andre Chenier

Reputation: 1186

Regex for a custom link (url) structure verification (preg_match, PHP)

I plan to allow reader to create links in a custom way. I think it will be easier for the ones who are not used to write html opening+closing tags.

allowed & required custom linking structure

++visible text part==invisible address part++

a valid structured example:
++stack overflow==http://stackoverflow.com/questions/ask++

my requirements

Of course a user can input more than 1 link.

  1. So I have to check every linking attempt. If all of links structure is formatted as ++visible text part==invisible address part++
  2. then I will need to verify & validate the invisible address part
  3. lastly I will strip the tags (if any or not) from visible text part

my question

So I require your help for the preg_match pattern to verify the custom linking structure. Please also be aware that I will need 2 parts (that are:left part of == visible text part AND right part of == invisible address part matched seperately for my 2nd and 3rd requirements.

my unsuccessful trial was

/++(.+)==(.+)++/

thanks, BR

Upvotes: 0

Views: 257

Answers (2)

MikeM
MikeM

Reputation: 13631

Try

/\+\+(.+?)==(.+?)\+\+/

+ is a regex special character and should be escaped.

Add s after the ending delimiter / if you want the match to include newline characters.

Upvotes: 2

kero
kero

Reputation: 10638

There are two major problems with your regex. First of all + is a reserved sign as you know (since you use it), so you have to escape it.

That would bring us to the following regex: /\+\+(.+)==(.+)\+\+/

If you use it, it might actually work, but only if the markup is used never or once, that is because regex in PHP is greedy. You can solve that by using the right modifier.

This brings us to /\+\+(.+)==(.+)\+\+/U which is not perfect but will work. You can then do other improvements regarding (.+)

Upvotes: 1

Related Questions