PHP:Breaking up a String

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

Post Reply
randomblink
Forum Commoner
Posts: 51
Joined: Wed Jan 28, 2004 11:27 am
Location: Tulsa, Oklahoma, just this side of hell...
Contact:

PHP:Breaking up a String

Post by randomblink »

Ok.
I did the pre-requisite searches from the SEARCH function here on the forums and found nothing. What I am asking is most likely easy, but is something I have never tried before. So...

Ok...
I have a string.

Code: Select all

<?php
$test = "id=2 border=0 width=1 style=color:red;width:100px class=patience insanity peace name=TryMe";
?>
Basically, the string holds Attributes as you would see in an HTML tag.
What I want to do is pull this string apart... For instance...

In $test? I would like create an array that looks like this:
Array
{
[id] => "2",
[border] => "0",
[width] => "1",
[style] => "color:red;width:100px",
[class] => "patience insanity peace",
[name] => "TryMe"
}

I can get ALL of the above EXCEPT the class one...
I just:

Code: Select all

<?php
$newArray = explode( " ", $test );
?>
But the moment I put the class in with the three words separated by spaces? I get a new newArray[insanity] = "" and newArray[peace] = ""... ugh!

I am thinking I will need a regular expression that searches out a single word before an equals sign... up to the next word in front of an equals sign. Where do I start...? Has anyone done this already? Help if you can...

Thanks...
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

Code: Select all

$str = "id=2 border=0 width=1 style=color:red;width:100px class=patience insanity peace name=TryMe";
preg_match_all("/(\w+)=/", $str, $keys);
$keys = $keys[1];
$vals = preg_split("/(\w+)=/", $str);
array_shift($vals);
$res = array();
foreach($keys as $id => $key)
    $res[$key] = trim($vals[$id]);
var_dump($res);
User avatar
PrObLeM
Forum Contributor
Posts: 418
Joined: Sun Mar 07, 2004 2:30 pm
Location: Mesa, AZ
Contact:

Post by PrObLeM »

Explode by =

then array[EVEN NUMBER] would be the varibles and the array[ODD NUMBER] or EVEN NUMBER+1 will be the value
randomblink
Forum Commoner
Posts: 51
Joined: Wed Jan 28, 2004 11:27 am
Location: Tulsa, Oklahoma, just this side of hell...
Contact:

Nice Try

Post by randomblink »

WEIRDAN:
I still have problems, but I think you are on the right track.
I will break this down some tonight...
I find problems in the array_shift... I am not sure what you are doing there... and the regex is the same for both preg_match_all and preg_split, BUT I do NOT understand regex enough to know if it is a mistake? Or supposed to be the same...

PROBLEM:
I thought about that...
But that doesn't work... Because you get:

Array
{
[0] => "id",
[1] => "2 border",
[2] => "0 width",
[3] => "1 style",
[4] => "color:red;width:100px class",
[5] => "patience insanity peace name"
[6] => "TryMe"
}

That doesn't work for me...
Good idea tho...
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Nice Try

Post by Weirdan »

randomblink wrote:WEIRDAN:
I still have problems, but I think you are on the right track.
I will break this down some tonight...
I find problems in the array_shift... I am not sure what you are doing there... and the regex is the same for both preg_match_all and preg_split, BUT I do NOT understand regex enough to know if it is a mistake? Or supposed to be the same...
They supposed to be the same. array_shift is there to get rid of the first (empty) element of $vals array. Have you tried my solution? for the string you provided it gives me:

Code: Select all

array(6) &#123;
  &#1111;"id"]=>
  string(1) "2"
  &#1111;"border"]=>
  string(1) "0"
  &#1111;"width"]=>
  string(1) "1"
  &#1111;"style"]=>
  string(21) "color:red;width:100px"
  &#1111;"class"]=>
  string(23) "patience insanity peace"
  &#1111;"name"]=>
  string(5) "TryMe"
&#125;
Post Reply