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

Reply

Join Date: Feb 2009
Posts: 3
Reputation: zerugaze is an unknown quantity at this point 
Solved Threads: 0
zerugaze zerugaze is offline Offline
Newbie Poster

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

 
0
  #1
Feb 21st, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 1,408
Reputation: ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light 
Solved Threads: 226
Sponsor
ShawnCplus's Avatar
ShawnCplus ShawnCplus is online now Online
Code Monkey

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

 
0
  #2
Feb 21st, 2009
^(.+?):(.+?)$ 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.
GCS d- s+ a-->? C++(++++) UL+++ P+>+++ L+++ E--- W+++
N+ o K w++(---) O? !M- V PS+>++ PE+ Y+ PGP !t- 5? X- R tv+
b+>++ DI+ D G++>+++ e+ h+>++ r y+
PMs asking for help will not be answered, post on the forums. That's what they're there for.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 10
Reputation: scrager is an unknown quantity at this point 
Solved Threads: 2
scrager scrager is offline Offline
Newbie Poster

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

 
0
  #3
Feb 21st, 2009
in this case I would use split.
  1. $parts = split(":", "table:column");
  2. $parts[0] //table
  3. $parts[1] //column
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 112
Reputation: !Unreal is an unknown quantity at this point 
Solved Threads: 2
!Unreal's Avatar
!Unreal !Unreal is offline Offline
Junior Poster

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

 
0
  #4
Feb 21st, 2009
Originally Posted by scrager View Post
in this case I would use split.
  1. $parts = split(":", "table:column");
  2. $parts[0] //table
  3. $parts[1] //column
Whats the difference between that and explode?
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 1,408
Reputation: ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light 
Solved Threads: 226
Sponsor
ShawnCplus's Avatar
ShawnCplus ShawnCplus is online now Online
Code Monkey

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

 
0
  #5
Feb 21st, 2009
Split and explode are the same thing, they are just two different names.
GCS d- s+ a-->? C++(++++) UL+++ P+>+++ L+++ E--- W+++
N+ o K w++(---) O? !M- V PS+>++ PE+ Y+ PGP !t- 5? X- R tv+
b+>++ DI+ D G++>+++ e+ h+>++ r y+
PMs asking for help will not be answered, post on the forums. That's what they're there for.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 112
Reputation: !Unreal is an unknown quantity at this point 
Solved Threads: 2
!Unreal's Avatar
!Unreal !Unreal is offline Offline
Junior Poster

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

 
1
  #6
Feb 21st, 2009
Hmm...Just googled it to double check and it said that split uses regex where as explode uses a string.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 524
Reputation: Will Gresham is on a distinguished road 
Solved Threads: 86
Sponsor
Will Gresham's Avatar
Will Gresham Will Gresham is offline Offline
Posting Pro

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

 
0
  #7
Feb 21st, 2009
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.
AJAX is not a programming language, scripting language or any other sort of language.
It is acheived by using JavaScript http functions.
So, AJAX = JavaScript.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 1,497
Reputation: cwarn23 has a spectacular aura about cwarn23 has a spectacular aura about cwarn23 has a spectacular aura about 
Solved Threads: 136
cwarn23's Avatar
cwarn23 cwarn23 is offline Offline
Nearly a Posting Virtuoso

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

 
0
  #8
Feb 21st, 2009
Originally Posted by !Unreal View Post
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.
Try not to bump 10 year old threads as it can be really annoying.
http://syntax.cwarn23.net/
Smilies: ^_* +_+ v_v -_- *~*`
My favourite PC. - MacGyver Fan
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 20
Reputation: jedi_ralf is an unknown quantity at this point 
Solved Threads: 2
jedi_ralf jedi_ralf is offline Offline
Newbie Poster

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

 
0
  #9
Feb 21st, 2009
Originally Posted by cwarn23 View Post
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/0...lode-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-co...ode-71423.html

So in answer to your question, preg_split uses regex like split. However, I don't know what the exact difference is.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC