Preg replace help

Status
Not open for further replies.

SJshah

Active Member
2,805
2009
524
20
I need a php function including a preg replace line (or two) which will:
- delete all brackets, apostrophe's, special characters
- replace numbers with spaces
- delete all words less than 3 characters
- trim the spaces

Unfortunately I'm a noob in php, and an uber-noob with regex.

Is there anyone here who can help me out?
 
17 comments
PHP:
<?php

function clean($txt)
{
    // remove everything that is not a word char, whitespace or a number
    $result = preg_replace('/[^\w\d\s]/', '', $txt);
    // replace numbers and trim spaces
    $result = preg_replace('/(\d+|[ ]{2,})/', ' ', $result);
    // remove words <= 3 chars
    $result = preg_replace('/\b[\w]{1,3}\b/', '', $result);
    
    return $result;
}

?>
Try that. Not sure if it works, never used regex in PHP.
 
Thanks Hyperz, its almost the way I want it :)

Could you make it keep all hyphens "-" in tact and also trim any extra spaces?
 
I mean any extra spaces, so if there is a double/triple space there it will be converted into a single space. Since the regex deletes all the numbers and words less than 3 chars, it leaves loads of extra spaces in once the string is returned.

I already tried
PHP:
    $result = trim($result, " ");
but it only removes the extra spaces on the ends
 
PHP:
<?php

function clean($txt)
{
    // remove everything that is not a word, _, -, whitespace or a number
    // remove words <= 3 chars
    $result = preg_replace('/([^\w\d\s\-]+|\b\w{1,3}\b)/', '', $txt);
    // replace numbers with a space
    $result = preg_replace('/\d+/', ' ', $result);
    // trim spaces
    $result = preg_replace('/[ ]{2,}/', ' ', $result);
    // trim spaces at the end or beginning of lines
    $result = preg_replace('/[ ]*(\r{0,1}\n)[ ]*/', '$1', $result);
    
    return $result;
}

?>
Had a little mistake in the other one too. Let me know if this does the trick.

Edit:
Added trimming of EOL spaces.
 
^ that fixes the spaces prob :)

but now it doesn't delete the words less than 3 chars from the string :/


edit: looks like you accidentally edited out this line:
PHP:
    // remove words <= 3 chars
    $result = preg_replace('/\b[\w]{1,3}\b/', '', $result);
added it back in, looks like it works perfectly :)

thanks m8
 
How about

PHP:
<?php

function clean($txt)
{
    // remove everything that is not a word, _, -, whitespace or a number
    $result = preg_replace('/[^\w\d\s\-]+/', '', $txt);
    // replace numbers with a space
    $result = preg_replace('/\d+/', ' ', $result);
    // remove words <= 3 chars
    $result = preg_replace('/\b\w{1,3}\b/', '', $result);
    // trim spaces
    $result = preg_replace('/[ ]{2,}/', ' ', $result);
    // trim spaces at the end or beginning of lines
    $result = preg_replace('/[ ]*(\r{0,1}\n)[ ]*/', '$1', $result);
    
    return $result;
}

?>
Grrr PHP X-(
 
Yup, seems to work now :) I used this to trim the spaces from the beginning and the end:
PHP:
    $result = trim($result);
But that brings up another problem, there are extra hyphens at the beginning and the end after the numbers/words under 3chars have been deleted. It needs to trim any remaining hyphens from the beginning and end too, before trimming the whitespace. I tried these lines but neither worked:
PHP:
    $result = trim($result, "-");
PHP:
    $result = preg_replace('/[-]*(\r{0,1}\n)[-]*/', '$1', $result);
 
trim($result) will not only trim spaces but also newlines (\r \n and others) I think. I'll just leave this for litewarez or someone else who is more familiar with PHP.
 
^ that shouldn't be a problem since they are only 1-liner's ;)

thanks for all the help m8


got it down to this:
PHP:
function clean($txt)
{
    // remove everything that is not a word, _, -, whitespace or a number
    $result = preg_replace('/([^\w\d\s\-]+|\b[\w]{1,3}\b)/', '', $txt);
    // replace numbers with a space
    $result = preg_replace('/\d+/', ' ', $result);
    // remove words <= 3 chars
    $result = preg_replace('/\b[\w]{1,3}\b/', '', $result);
    // trim spaces
    $result = preg_replace('/[ ]{2,}/', ' ', $result);
    $result = trim($result);
    
    return $result;
}
 
Screw it, bored anyways:
PHP:
<?php

function clean($txt)
{
    // remove everything that is not a word, _, -, whitespace or a number
    $result = preg_replace('/[^\w\d\s\-]+/', '', $txt);
    // replace numbers with a space
    $result = preg_replace('/\d+/', ' ', $result);
    // remove words <= 3 chars
    $result = preg_replace('/\b\w{1,3}\b/', '', $result);
    // trim spaces
    $result = preg_replace('/[ ]{2,}/', ' ', $result);
    // trim -
    $result = preg_replace('/-{2,}/', '-', $result);
    // trim spaces and - at the end or beginning of lines
    $result = preg_replace('/[ \-]*(\r{0,1}\n)[ \-]*/', '$1', $result);
    
    return $result;
}

?>

FYI, $result = trim($result); doesn't do the same thing as the line you replaced it with.
 
Yeah, but this line doesn't seem to work:
PHP:
    // trim spaces and - at the end or beginning of lines
    $result = preg_replace('/[ \-]*(\r{0,1}\n)[ \-]*/', '$1', $result);
Since its not trimming the spaces from the ends :/

trim($result) seems to do the trick.
 
PHP:
<?php

function clean($txt)
{
    // remove everything that is not a word, _, -, whitespace or a number
    $result = preg_replace('/[^\w\d\s\-]+/', '', $txt);
    // replace numbers with a space
    $result = preg_replace('/\d+/', ' ', $result);
    // remove words <= 3 chars
    $result = preg_replace('/\b\w{1,3}\b/', '', $result);
    // trim spaces
    $result = preg_replace('/[ ]{2,}/', ' ', $result);
    // trim -
    $result = preg_replace('/-{2,}/', '-', $result);
    // trim spaces and - at the end or beginning of lines
    $result = preg_replace('/[ \-]*(\r{0,1}\n)[ \-]*/', '$1', $result);
    
    return trim($result, '- \t\n\r\0\x0B');
}

?>
The regex only trimmed spaces where there were new lines, not at the beginning or end of the string (which is what trim does).
 
Status
Not open for further replies.
Back
Top