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

Sunday, November 20, 2022

[FIXED] How to solve string pattern puzzle by using preg_replace function?

 November 20, 2022     php, preg-replace     No comments   

Issue

I am not familiar with preg_replace function of php. I need to solve an issue by using this function.

My string is sidebarCond/none.php. I need to remove folder name whatever it is (like: sidebarCond/ or folder-name/) and also the extension (Like: .php or .html).

$sidebar = 'sidebarCond/none.php';
$sidebar = str_replace( 'sidebarCond/', '', $sidebar );
$sidebar = str_replace( '.php', '', $sidebar );

This is the code of how I solve my problem, but this is not a smart solution.


Solution

Don't use string functions in this case. Use pathinfo

$sidebar = 'sidebarCond/none.php';
$sidebar = pathinfo($sidebar)['filename'];

OR

$sidebar = 'sidebarCond/none.php';
$sidebar = pathinfo($sidebar, PATHINFO_FILENAME);


Answered By - ksiger
Answer Checked By - David Goodson (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