Page 1 of 3
Split string and retain part before character match?
Posted: Fri Jun 13, 2008 6:37 am
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...
...and I don't want an array, just to retain a variable.
Re: Split string and retain part before character match?
Posted: Fri Jun 13, 2008 8:00 am
by superdezign
preg_match()?
Code: Select all
preg_match('~^(.*?)(' . $yourRegex . ')$~', $myVar1, $someArray);
Re: Split string and retain part before character match?
Posted: Fri Jun 13, 2008 8:00 am
by JAB Creations
Ha! I guess the red bull hadn't taken effect when I originally posted this!
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];
Re: Split string and retain part before character match?
Posted: Fri Jun 13, 2008 8:01 am
by JAB Creations
Mid-air collision!
Thanks for your suggestion!
Re: Split string and retain part before character match?
Posted: Fri Jun 13, 2008 11:40 am
by RobertGonzalez
You could always use substr() in conjunction with strpos()
Re: Split string and retain part before character match?
Posted: Fri Jun 13, 2008 11:55 am
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.
Re: Split string and retain part before character match?
Posted: Fri Jun 13, 2008 12:13 pm
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.
Re: Split string and retain part before character match?
Posted: Fri Jun 13, 2008 12:16 pm
by RobertGonzalez
Thanks Kieran. That is exactly what I meant.
Re: Split string and retain part before character match?
Posted: Fri Jun 13, 2008 12:26 pm
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.

Re: Split string and retain part before character match?
Posted: Fri Jun 13, 2008 1:01 pm
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.
Re: Split string and retain part before character match?
Posted: Fri Jun 13, 2008 2:03 pm
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.
Re: Split string and retain part before character match?
Posted: Fri Jun 13, 2008 2:17 pm
by Zoxive
"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;
Re: Split string and retain part before character match?
Posted: Fri Jun 13, 2008 2:20 pm
by Kieran Huggins
fixed, thanks

Re: Split string and retain part before character match?
Posted: Fri Jun 13, 2008 3:15 pm
by JAB Creations
Hmm I'm a bit lost...
Currently...
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?
Re: Split string and retain part before character match?
Posted: Fri Jun 13, 2008 3:54 pm
by RobertGonzalez
What is the value of the var you want to get the parts of, and from what point?