Re: [PHP] Problem with Regular expression
* Thus wrote kioto: Hi all. I have a problem: i want subs any characters from a string but i don't have fix the problem. The string that i want to manipulate is the value from a text field of a search engine. The characters that i want to try substitute is , , +, -, |, ||, or, and, not in not case-sensitive mode with . I have create a pattern like this: $str = Sybase and PHP not ASP or JSP Oracle not Andy | Perl || Python + Ruby; $pattern = /(\band\b)|(\bnot\b)|(\bor\b)|(\b\b)|(\b\b)|(\b\.\b)|(\b\+\b)|(\b\|\|\b)|(\b\|\b)/i; echo $str = preg_replace($pattern, , $str, -1); But characters like + don't subs with . Thanks to all and sorry my bad language thats because + | are word boundaries. Also, you have to many sub patterns, you dont need all the () around each item, wrap the whole expression in (). And finally, you dont take into consideration of spaces, around each item you have a space so you'll end up with two spaces between each word. Your final expression should be: $pattern = /\s*(\band\b|\bnot\b|\bor\b|||\b\.\b|\+|\|\||\|)\s*/i; And replace all that with one space: $str = preg_replace($pattern, ' ', $str); HTH, Curt -- Quoth the Raven, Nevermore. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Problem with Regular expression
Hi all. I have a problem: i want subs any characters from a string but i don't have fix the problem. The string that i want to manipulate is the value from a text field of a search engine. The characters that i want to try substitute is , , +, -, |, ||, or, and, not in not case-sensitive mode with . I have create a pattern like this: $str = Sybase and PHP not ASP or JSP Oracle not Andy | Perl || Python + Ruby; $pattern = /(\band\b)|(\bnot\b)|(\bor\b)|(\b\b)|(\b\b)|(\b\.\b)|(\b\+\b)|(\b\|\|\b)|(\b\|\b)/i; echo $str = preg_replace($pattern, , $str, -1); But characters like + don't subs with . Thanks to all and sorry my bad language -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Problem with Regular expression
Try with \s instead of \b, I meant: $pattern = /(\sand\s)|(\snot\s)|(\sor\s)|(\s\s)|(\s\s)|(\s\.\s)|(\s\+\s)|(\s\|\|\s)|(\s\|\s)/i; kioto wrote: Hi all. I have a problem: i want subs any characters from a string but i don't have fix the problem. The string that i want to manipulate is the value from a text field of a search engine. The characters that i want to try substitute is , , +, -, |, ||, or, and, not in not case-sensitive mode with . I have create a pattern like this: $str = Sybase and PHP not ASP or JSP Oracle not Andy | Perl || Python + Ruby; $pattern = /(\band\b)|(\bnot\b)|(\bor\b)|(\b\b)|(\b\b)|(\b\.\b)|(\b\+\b)|(\b\|\|\b)|(\b\|\b)/i; echo $str = preg_replace($pattern, , $str, -1); But characters like + don't subs with . Thanks to all and sorry my bad language -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Problem with Regular expression
Hi, Wednesday, October 27, 2004, 9:04:14 PM, you wrote: k Hi all. k I have a problem: i want subs any characters from a string but i don't k have fix the problem. k The string that i want to manipulate is the value from a text field of a k search engine. k The characters that i want to try substitute is , , +, -, |, ||, or, k and, not in not case-sensitive mode with . k I have create a pattern like this: k $str = Sybase and PHP not ASP or JSP Oracle not Andy | Perl || k Python + Ruby; k $pattern = k /(\band\b)|(\bnot\b)|(\bor\b)|(\b\b)|(\b\b)|(\b\.\b)|(\b\+\b)|(\b\|\|\b)|(\b\|\b)/i; k echo $str = preg_replace($pattern, , $str, -1); k But characters like + don't subs with . k Thanks to all and sorry my bad language this may get you started preg_replace('!\s(and|not|or||\||\|\||)\s|(\+|-\s*)!i',' ',$string); you have to replace with ' ' or the words will run into each other and you may also get the + or - with no trailing space. -- regards, Tom -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php