1

All hail Regex!

In PHP: From this:

urlencode($row_rs_details['Title'],ENT_QUOTES, 'utf-8');

and several hundred more where the variable names are different, I need to remove the ,ENT_QUOTES, 'utf-8' and just have this:

 urlencode($row_rs_details['Title']);

while leaving the many different $row_rs_names intact.

Sincere thanks for saving me at least six hours.

2
  • 1
    Are you looking for something like this? See a regex demo here. Commented Oct 25, 2015 at 19:07
  • 1
    @stribizhev Hold on, that should do it. Please post this as your answer and I'll upvote it. Thanks. Commented Oct 25, 2015 at 19:18

2 Answers 2

1

You may use the following regex:

(urlencode\(\$[^][]*\['Title'\]),ENT_QUOTES,\s*'utf-8'\);

And replace with $1);.

See regex demo

It is basically matches and captures the first part beginning from urlencode and up to and including ['Title'], the rest is matched, but is later removed during the replace operation. The captured text is restored in the result with the $1 back-reference and a literal );.

Here is a PHP code demo:

$re = '/(urlencode\(\$[^][]*\[\'Title\'\]),ENT_QUOTES,\s*\'utf-8\'\);/'; 
$str = "urlencode(\$row_rs_details['Title'],ENT_QUOTES, 'utf-8');"; 
$subst = "$1);"; 
echo $result = preg_replace($re, $subst, $str, 1);

Result: urlencode($row_rs_details['Title']);

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for that help. Tip of the hat to the wizards.
1

Occam's razor. You don't need regex for this. Just do a simple find and replace for the search string urlencode($row_rs_details['Title'],ENT_QUOTES, 'utf-8'); with urlencode($row_rs_details['Title']); as the target string.

12 Comments

There are hundreds of others with that which don't need changed. I'm specifically looking for those with 'Title'
@AllThisOnAnACER: Updated the answer. If you need something more specific, please consider providing a bigger snippet of the text you'll be processing.
Well there are hundreds of these very similarly coded. Some are in url's some are in pages. I don't want to find them all and screw up most of them, I simply want to target those that have the aforementioned format. Thanks for your help
@AllThisOnAnACER: Please update the question so it contains enough information to answer it. We don't need an exhaustive list of examples, but we do need a description that lets us know which parts of the string can vary and how. (While you're at it, you might want to improve the title, and perhaps specify what language and/or library you're using; there are a lot of different varieties of regular expressions.)
@AllThisOnAnACER: Sorry, but your examples do not seem sufficiently clear. Could you add a bigger snippet to your question? What are some examples where the regex should not identify the search string?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.