954,585 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

What is the php regular expression to get everything before a certain character?

For instance, I have the following string:

table:column

I need to get "table" as the result of the php regular expression.

So, I need everything before the ":" symbol

After that regular expression is performed, I would also like to get everything after the ":" symbol, or "column" as well.

zerugaze
Newbie Poster
3 posts since Feb 2009
Reputation Points: 10
Solved Threads: 0
 

^(.+?):(.+?)$
That's pretty much it. That's a pretty simple regex, you might want to study them for yourself http://www.regular-expressions.info/ , it's a pretty decent resource.

ShawnCplus
Code Monkey
Team Colleague
1,583 posts since Apr 2005
Reputation Points: 526
Solved Threads: 268
 

in this case I would use split.

$parts = split(":", "table:column");
$parts[0] //table
$parts[1] //column
scrager
Newbie Poster
10 posts since Feb 2009
Reputation Points: 12
Solved Threads: 2
 

in this case I would use split.

$parts = split(":", "table:column");
$parts[0] //table
$parts[1] //column

Whats the difference between that and explode?

!Unreal
Junior Poster
112 posts since Dec 2007
Reputation Points: 11
Solved Threads: 2
 

Split and explode are the same thing, they are just two different names.

ShawnCplus
Code Monkey
Team Colleague
1,583 posts since Apr 2005
Reputation Points: 526
Solved Threads: 268
 

Hmm...Just googled it to double check and it said that split uses regex where as explode uses a string.

!Unreal
Junior Poster
112 posts since Dec 2007
Reputation Points: 11
Solved Threads: 2
 

That and explode is supposed to execute faster due to the lack of regex, although for what the OP wants, I doubt it will make much difference which is used.

Will Gresham
Master Poster
755 posts since May 2008
Reputation Points: 96
Solved Threads: 125
 
Hmm...Just googled it to double check and it said that split uses regex where as explode uses a string.


If explode uses a string and split uses regex then what does preg_split use? What is the difference between split and preg_split? Just curious.

cwarn23
Occupation: Genius
Team Colleague
3,033 posts since Sep 2007
Reputation Points: 413
Solved Threads: 259
 
If explode uses a string and split uses regex then what does preg_split use? What is the difference between split and preg_split? Just curious.

"explode() takes a delimiter to split by ... split() takes a regular expression"
"documentation says that preg_split() is faster than split()" http://blog.brianhartsock.com/2007/06/11/php-explode-vs-split/

"preg_split(), which uses a Perl-compatible regular expression syntax, is often a faster alternative to split(). If you don't require the power of regular expressions, it is faster to use explode(), which doesn't incur the overhead of the regular expression engine."
- http://uk3.php.net/split

Some speed stats: http://forums.codewalkers.com/php-coding-7/split-vs-preg-split-vs-explode-71423.html

So in answer to your question, preg_split uses regex like split. However, I don't know what the exact difference is.

jedi_ralf
Newbie Poster
20 posts since Apr 2008
Reputation Points: 10
Solved Threads: 3
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You