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

Sunday, November 20, 2022

[FIXED] How to remove multiple slashes in URI with 'PREG'

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

Issue

I am using str_replace() for removing extra slashes from url i dont know how to redirect the url to new url if find multi slashes in url?

if(str_replace(':/','://', trim(preg_replace('/\/+/', '/', PERMALINK), '/')))
{
  echo 'Yes found multi slashes redirect it to new url';
}
else
{
  echo 'Not found multi slashes';
}

Solution

using the plus symbol + in regex means the occurrence of one or more of the previous character. So we can add it in a preg_replace to replace the occurrence of one or more / by just one of them

$url = "site.com/edition/new///";

$newUrl = preg_replace('/(/+)/','/',$url);

// now it should be replace with the correct single forward slash echo $newUrl



Answered By - Engr Moazam Haris
Answer Checked By - Pedro (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