Split string and retain part before character match?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Split string and retain part before character match?

Post by JAB Creations »

What would be the best function to use in order to split a string and retain the smaller segment that occurs before a character match within the original string?

In example I have a selector and it's associated properties and values but I just want to crop off everything from the initial curely bracket.

So I want to turn this...

Code: Select all

$my_var1 = '#welcome  {background-color: #111; background-image: url(general/11/001.gif); border-color: #99f; color: #ccc;';
...in to this...

Code: Select all

$my_var2 = '#welcome';
...and I don't want an array, just to retain a variable.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: Split string and retain part before character match?

Post by superdezign »

preg_match()?

Code: Select all

preg_match('~^(.*?)(' . $yourRegex . ')$~', $myVar1, $someArray);
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: Split string and retain part before character match?

Post by JAB Creations »

Ha! I guess the red bull hadn't taken effect when I originally posted this! :mrgreen:

Code: Select all

$str = '#welcome, #content div.border, #content div.bordernorc, #prompts div.scroll, #prompts #promptstabs div.current {background-color: #111; background-image: url(general/11/001.gif); border-color: #99f; color: #ccc;';
$my_var = split('{', $str);
echo $my_var[0].'<br />'.$my_var[1];
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: Split string and retain part before character match?

Post by JAB Creations »

Mid-air collision! :mrgreen:

Thanks for your suggestion!
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: Split string and retain part before character match?

Post by RobertGonzalez »

You could always use substr() in conjunction with strpos()
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: Split string and retain part before character match?

Post by JAB Creations »

Everah wrote:You could always use substr() in conjunction with strpos()
That is a good suggestion however while I did take them in to consideration I won't be dealing with a number position with the variables but rather needing to split at fluctuating lengths where a character is matched.

Unless I'm missing something in which case I'd be interesting in further suggestions? Though I don't perceive how I could use two functions in conjunction to attain my goal.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Re: Split string and retain part before character match?

Post by Kieran Huggins »

Everah meant:

Code: Select all

$str = "abcdefg";
$before = substr(0,strpos($str,'c'));
It's much faster than regular expressions, not that it's usually a big deal. Just making it work is priority #1.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: Split string and retain part before character match?

Post by RobertGonzalez »

Thanks Kieran. That is exactly what I meant.
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: Split string and retain part before character match?

Post by JAB Creations »

So two functions combined are still more efficient then using split (regex essentially) eh?

Thanks to both of you, Kieran for the working example and Everah for the useful suggestion.

I just had a thought, if this suggestion creates less load then it uses less energy...so in effect you guys could be considered green coders. :mrgreen:
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: Split string and retain part before character match?

Post by RobertGonzalez »

I rather prefer blue, but thanks.

And yes, strpos() is hugely less intensive, even coupled with strstr() and more functions, than the regex functions.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Re: Split string and retain part before character match?

Post by Kieran Huggins »

it's not easy being green.

Regex has a huge overhead in both memory and CPU time, whereas simple string functions are practically free.

That being said, a sexy regex can do feats of near magic.... they both have their uses.
Last edited by Kieran Huggins on Fri Jun 13, 2008 2:20 pm, edited 1 time in total.
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Re: Split string and retain part before character match?

Post by Zoxive »

Kieran Huggins wrote:it's not easy being green.
"The URL contained a malformed video ID."
Kieran Huggins wrote: Regex has a huge overhead in both memory and CPU time, whereas simple string functions are practically free.

That being said, a sexy regex can do feats of near magic.... they both have their uses.
++$Kieran;
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Re: Split string and retain part before character match?

Post by Kieran Huggins »

fixed, thanks :-)
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: Split string and retain part before character match?

Post by JAB Creations »

Hmm I'm a bit lost...

Currently...

Code: Select all

$$my_var2[3] == 'color: #333'
But when I execute code such as...

Code: Select all

$my_var1 = substr(0xyz,strpos($my_var2[3],'{'));
echo $my_var1;
All I get echoed is the first number after substr? (I inserted xyz after that number just for quick visual reference)

In this instance I desire to echo $my_var1 || $my_var1[0] || $my_var1[1] and have PHP echo '333'. What am I missing here?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: Split string and retain part before character match?

Post by RobertGonzalez »

What is the value of the var you want to get the parts of, and from what point?
Post Reply