Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.php > #8726 > unrolled thread
| Started by | CloudPress <cloudpresshosting@gmail.com> |
|---|---|
| First post | 2013-03-18 21:12 -0700 |
| Last post | 2013-03-19 21:43 -0400 |
| Articles | 8 — 3 participants |
Back to article view | Back to comp.lang.php
split array and string from string (trust me it will make sense when you read) CloudPress <cloudpresshosting@gmail.com> - 2013-03-18 21:12 -0700
Re: split array and string from string (trust me it will make sense when you read) CloudPress <cloudpresshosting@gmail.com> - 2013-03-18 21:17 -0700
Re: split array and string from string (trust me it will make sense when you read) Jerry Stuckle <jstucklex@attglobal.net> - 2013-03-19 09:06 -0400
Re: split array and string from string (trust me it will make sense when you read) CloudPress <cloudpresshosting@gmail.com> - 2013-03-19 17:34 -0700
Re: split array and string from string (trust me it will make sense when you read) Jerry Stuckle <jstucklex@attglobal.net> - 2013-03-19 20:45 -0400
Re: split array and string from string (trust me it will make sense when you read) CloudPress <cloudpresshosting@gmail.com> - 2013-03-19 17:56 -0700
Re: split array and string from string (trust me it will make sense when you read) Christoph Becker <cmbecker69@gmx.de> - 2013-03-20 02:21 +0100
Re: split array and string from string (trust me it will make sense when you read) Jerry Stuckle <jstucklex@attglobal.net> - 2013-03-19 21:43 -0400
| From | CloudPress <cloudpresshosting@gmail.com> |
|---|---|
| Date | 2013-03-18 21:12 -0700 |
| Subject | split array and string from string (trust me it will make sense when you read) |
| Message-ID | <a9993724-7709-41e0-9412-02ed3056d12d@googlegroups.com> |
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?
[toc] | [next] | [standalone]
| From | CloudPress <cloudpresshosting@gmail.com> |
|---|---|
| Date | 2013-03-18 21:17 -0700 |
| Message-ID | <cdb74360-50d6-4c31-882f-77d8c8fd22d0@googlegroups.com> |
| In reply to | #8726 |
I know of a way where I can step though looking for each instance of "[" reading between that and the next "=" and that is my key, then read between the "=" and the "]" for the value. Lead each of those sub strings into an array. Then do a str_ireplace to replace [$key=$value] with "". Just curious if there is an easier way.
On Monday, March 18, 2013 9:12:28 PM UTC-7, 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?
[toc] | [prev] | [next] | [standalone]
| From | Jerry Stuckle <jstucklex@attglobal.net> |
|---|---|
| Date | 2013-03-19 09:06 -0400 |
| Message-ID | <ki9np5$2ll$3@dont-email.me> |
| In reply to | #8726 |
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@attglobal.net
==================
[toc] | [prev] | [next] | [standalone]
| From | CloudPress <cloudpresshosting@gmail.com> |
|---|---|
| Date | 2013-03-19 17:34 -0700 |
| Message-ID | <3514efbb-74a4-41e7-8fac-2092a94cbc95@googlegroups.com> |
| In reply to | #8736 |
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@attglobal.net
>
> ==================
[toc] | [prev] | [next] | [standalone]
| From | Jerry Stuckle <jstucklex@attglobal.net> |
|---|---|
| Date | 2013-03-19 20:45 -0400 |
| Message-ID | <kib0o8$sp9$1@dont-email.me> |
| In reply to | #8747 |
On 3/19/2013 8:34 PM, CloudPress wrote:
> 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?
>>
>>
> 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?
>
That's the hard way. Look up a regex tutorial - there are several good
ones on the internet. What you're looking for shouldn't be that hard
(even I could probably come up with something even though I'm by far not
an expert in regex).
Then look at the preg_xxx() functions in PHP.
The only thing to remember is in PHP you need a separator character at
the start and end of the regex string, i.e. '!regex goes here!'. It
doesn't have to be an ! - it can be any character, preferably one not in
the regex.
And please don't top post. This newsgroup (and most of usenet) uses
bottom or interleaved posting. Thanks.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
[toc] | [prev] | [next] | [standalone]
| From | CloudPress <cloudpresshosting@gmail.com> |
|---|---|
| Date | 2013-03-19 17:56 -0700 |
| Message-ID | <79259447-b171-4dc0-8a8b-4a0ee86aeae0@googlegroups.com> |
| In reply to | #8748 |
I guess the real question is is there anything to be gained by using regular expressions. besides cleaner code? Because if this will work without any noticeable performance difference compared to regex it might just be easier to stick to this.
[toc] | [prev] | [next] | [standalone]
| From | Christoph Becker <cmbecker69@gmx.de> |
|---|---|
| Date | 2013-03-20 02:21 +0100 |
| Message-ID | <kib2tl$v84$1@speranza.aioe.org> |
| In reply to | #8749 |
CloudPress wrote:
> I guess the real question is is there anything to be gained by using
> regular expressions. besides cleaner code? Because if this will work
> without any noticeable performance difference compared to regex it might
> just be easier to stick to this.
IMO it's not so much about cleaner code, than about performance. Using
regexp's will shift all the hard work from PHP to C, which is much
faster. Consider the code you've posted: it calls strpos(), substr(),
str_replace() et al. in a loop multiple times; for sure not the fastest
way of doing things.
A quick example, what can be accomplished with a simple regexp:
>>> $str = 'Hello world, [key=value] [key2=value1] This is just a
test. [key=value2]';
>>> echo preg_replace('/\[key[0-9]*=value[0-9]*\]/', '', $str);
Hello world, This is just a test.
To fill the array of key/value pairs, you can start with:
>>> preg_match_all('/\[(key[0-9]*)=(value[0-9]*)\]/', $str, $m);
>>> var_dump($m)
--
Christoph M. Becker
[toc] | [prev] | [next] | [standalone]
| From | Jerry Stuckle <jstucklex@attglobal.net> |
|---|---|
| Date | 2013-03-19 21:43 -0400 |
| Message-ID | <kib44n$b3r$1@dont-email.me> |
| In reply to | #8749 |
On 3/19/2013 8:56 PM, CloudPress wrote: > I guess the real question is is there anything to be gained by using regular expressions. besides cleaner code? Because if this will work without any noticeable performance difference compared to regex it might just be easier to stick to this. > Cleaner code is (almost) always better. The more work I can have the system do, the easier it is to code and maintain. Fewer "gotchas" also. -- ================== Remove the "x" from my email address Jerry Stuckle JDS Computer Training Corp. jstucklex@attglobal.net ==================
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.php
csiph-web