FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » split array and string from string (trust me it will make sense when you read)
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Re: split array and string from string (trust me it will make sense when you read) [message #180811 is a reply to message #180800] Wed, 20 March 2013 00:34 Go to previous messageGo to previous message
cph is currently offline  cph
Messages: 10
Registered: September 2012
Karma:
Junior Member
Truth be told I don't know anything about regex. I threw together an example of how I think I can step though a string to do this.

$actions = array();
$done = false;
while (!$done) {
$start = strpos($content, "[");
$end = strpos($content, "]");
$equal = strpos($content, "=");
$key = "";
$value = "";
if ($equal != false && $equal > $start && $equal < $end) {
$key = substr($content, $start + 1, $equal - $start);
$value = substr($content, $equal + 1, $end - $equal);
if (array_key_exists($key, $actions)) {
if (is_array($actions[$key])) {
$actions[$key][] = $value;
}else{
$existing_value = $actions[$key];
$actions[$key] = array ($existing_value, $value);
}
}else{
$actions[$key] = $value;
}
str_replace("[" . $key . "=" . $value . "]", "", $content, 1);
}else{
$key = substr($content, $start + 1, $end - $start);
$actions[$key] = true;
str_replace("[" . $key . "]", "", $content, 1);
}
if ($start == false || $end == false) {
$done = true;
}
}

This should...in theory find the first set of brackets if there is a "=" it should separate out the part before the = as the key and the part after as the value, add to an array, using a sub array if the same key is duplicated with different values. If not it will just store the text as the key with a value of true. It will then replace the full sub-string from [ to ] with null effectively removing it then search for the next one until it finds no more bracket sets.

Any feedback, ideas on a better implementation?

On Tuesday, March 19, 2013 6:06:28 AM UTC-7, Jerry Stuckle wrote:
> On 3/19/2013 12:12 AM, CloudPress wrote:
>
>> I have a string that consists of several key value pairs such as [key=value], a given key may appear more than once.
>
>>
>
>> For example a string of:
>
>>
>
>> "Hello world, [key=value] [key2=value1] This is just a test. [key=value2]"
>
>>
>
>> I want to do two things, one I want an easier way to view the key value pairs, I am thinking an array
>
>>
>
>> array ('key' => array ('value', 'value2'), 'key2' => 'value1');
>
>>
>
>> Secondly I want the key/value pairs removed from the string so I would get the array and a new string that would look like this:
>
>>
>
>> "Hello world, This is just a test."
>
>>
>
>> What would be the easiest way to accomplish this?
>
>>
>
>
>
> Maybe a regex?
>
>
>
> --
>
> ==================
>
> Remove the "x" from my email address
>
> Jerry Stuckle
>
> JDS Computer Training Corp.
>
> jstucklex(at)attglobal(dot)net
>
> ==================
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: Thorny string translation issue.
Next Topic: PDO based Abstraction Layer for MySQL with Caching
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Sat Nov 30 10:21:22 GMT 2024

Total time taken to generate the page: 0.04484 seconds