Jump to content

Only use preg_replace in between quotations...


ryancooper

Recommended Posts

if (stripos($data, 'numbers') !== false) {
$data = preg_replace('/\d/', '', $data);
}

 

This is the code i was able to come up with some help from the people at sof... It's supposed to remove the numbers mixed in characters in between quotations if a keyword is found. In the example above that word is numbers, however this example removes all numbers from the entire string and if i change the regex to '/"\d+"/' to only remove numbers matched within quotes it no longer works. This may or may not need to be moved, i realize the regex maybe the problem, however i wasnt sure if the quotation section needed to be defined as a new object to have the preg_replace applied to and the reinserted back in place, thus leaving the numbers not in quotations alone.

 

A example would be: I am 13 as of today, what an unlucky number. "B1D3AY" Would be I am 13 as of today, what an unlucky number. "BDAY" Stripping the 1 and 3 from BDAY but leaving the 13 not inside quotations.

Link to comment
Share on other sites

try

<?php
$test = 'I am 13 as of today, what an unlucky number. "B1D3AY"';
function remove_numbers($a){
return preg_replace('/\d/','', $a[0]);
}
echo preg_replace_callback('/"[^"]+"/','remove_numbers',$test);
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.