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

Sunday, November 20, 2022

[FIXED] How to delete string with regex preg_replace php

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

Issue

i have full string like this:

Route::post('asdasdasdad/{param1}/{param2}', 'Admin\RouteController@a212e12e');.

and want to delete that route so in preg_replace i focus on

Route::post('asdasdasdad as start text and

Admin\RouteController@a212e12e'); as last text.

here what i try

preg_replace("/Route::post('asdasdasdad\(.*Admin\RouteController@a212e12e');\s*/s", "", $string);

but its not working.


Solution

you have some errors in your regex, some un-escaped regex characters. try this

preg_replace("/Route::post\('asdasdasdad.*Admin\\\\RouteController@a212e12e'\);\s*/s", "", $string); 

if you want to replace multiple lines in one go

preg_replace_all("/Route::post\('asdasdasdad.*Admin\\\\RouteController@a212e12e'\);\s*/s", "", $string); 

witch works as if you add the multi line modifier to your regex

$string = file_get_contents('route.php');
$string = preg_replace("/Route::post\('asdasdasdad.*Admin\\\\RouteController@a212e12e'\);\s*/s", "", $string);
echo $string;

you get the line with EOL removed



Answered By - N69S
Answer Checked By - Willingham (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