PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, November 20, 2022

[FIXED] Why does my regex return empty in the capturing subpattern?

 November 20, 2022     php, preg-replace, regex     No comments   

Issue

I have written these codes to convert url-like text to hyperlink:

$reg_exUrl = "/(http|https|ftp|ftps)\:\/\/www\.[a-zA-Z0-9]{5}\.com(\w*)(\/\S*)?/";
echo preg_replace($reg_exUrl, '<a href="$1://www.$2.com">$1://www.$2.com$3</a>','http://www.abcde.com/index.html');

Output is :

<a href="http://www..com">http://www..com/index.html</a>

But $2 is empty - why is this?


Solution

I think you want something like this:

(http|https|ftp|ftps)\:\/\/www\.([a-zA-Z0-9]{5})\.com(\w*\/\S*)?

https://regex101.com/r/MJ3A6K/1



Answered By - amasmiller
Answer Checked By - Mary Flores (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing