Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-23-2013, 03:16 PM   PM User | #1
Coastal Web
Regular Coder

 
Coastal Web's Avatar
 
Join Date: Oct 2004
Posts: 225
Thanks: 12
Thanked 3 Times in 3 Posts
Coastal Web is an unknown quantity at this point
quick help with preg_replace?

Hi guys, l have a few strings for example:

testing+123
testing+1+2+3

What l'd like to do is strip off the + signs and anything after them,
So in this example both strings would become 'testing'

Can anyone help me with a preg_replace that would solve this problem for me?

Regards,
Coastal Web is offline   Reply With Quote
Old 01-23-2013, 04:28 PM   PM User | #2
tempz
Regular Coder

 
Join Date: Jul 2012
Location: London
Posts: 436
Thanks: 4
Thanked 80 Times in 80 Posts
tempz is an unknown quantity at this point
PHP Code:
<?php
function myUrlEncode($string)
{
    return 
urlencode(str_replace'+' ' ' $string ));
}
?>
tempz is offline   Reply With Quote
Old 01-23-2013, 04:38 PM   PM User | #3
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,653
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
That's the right idea, but not quite what they are looking for. Sounds like they want to remove everything to the right and including the first + sign.
There are several ways. I'd use one of the ones below given the simple criteria:
PHP Code:
$aParts explode('+'$str);
print 
$aParts[0]; 
Would be the easiest.
PHP Code:
$sFirst strtok($str'+');
print 
$sFirst
Is another easy one and probably slightly better on memory.

There are several ways to accomplish this; it is one of the pros of being a string based language. I would not recommend using pcre when you are simply striping based off of another character.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Old 01-23-2013, 07:49 PM   PM User | #4
Coastal Web
Regular Coder

 
Coastal Web's Avatar
 
Join Date: Oct 2004
Posts: 225
Thanks: 12
Thanked 3 Times in 3 Posts
Coastal Web is an unknown quantity at this point
Thanks, l was specifically looking to do with preg_replace.
Here's what the winning solution is:
PHP Code:
    $str preg_replace('/\+.*$/'''$str); 
Coastal Web is offline   Reply With Quote
Old 01-23-2013, 08:11 PM   PM User | #5
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,653
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
I'm not sure I understand why you want regex.
Pattern matching is substantially slower than tokenizing by orders of magnitude (albeit we're talking about thousandths of seconds, but still).
For such a simple match, its like swatting a fly with a sledgehammer.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Tags
preg_replace, strings

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:25 AM.


Advertisement
Log in to turn off these ads.