php splitting up strings

Associate
Joined
4 Mar 2007
Posts
315
Location
United Kingdom
Ok so I have found through a very annoying way that my system will not work on my own server as it's using php 5.2 and my hosts refuse to update. Thus I cannot use the boolean within strstr and it has to be used with the 2 arguments instead of 3 strstr($stringName, ':', true);

This means now I cannot find elements of a string on the left or right, just one side.

For instance my string structure is:

firstElement&secondElement:thirdElement

I initially had it doing this:

PHP:
$_first = strstr($myString, ":", true);
$third = strstr($myString, ":", false);
$first = strstr($_first, '&', true);
$second = strstr($_first, '&', false);

but I cannot do this as I get a warning which is a bug with anything from 5.2 as it only works with 5.3 are there any other ways I can do this?

Regards,
 
Soldato
Joined
14 Feb 2006
Posts
4,644
Location
Surrey, UK
PHP:
$initial = "first&second:third";
$amp_pieces = explode("&", $initial);
$first = $amp_pieces[0];
$c_pieces = explode(":", $amp_pieces[1]);
$second = $c_pieces[0];
$third = $c_pieces[1];

Messy I know, but should work...
 

fez

fez

Caporegime
Joined
22 Aug 2008
Posts
25,023
Location
Tunbridge Wells
GeForces method should work perfectly. Is there any reason why you are using different separation characters for the 3 parts? Do you have no control over the format of the string?
 
Associate
OP
Joined
4 Mar 2007
Posts
315
Location
United Kingdom
GeForces method should work perfectly. Is there any reason why you are using different separation characters for the 3 parts? Do you have no control over the format of the string?

Basically the system uses two types of delimiters which are assigned from a relationship tree that I have no control over. These are put together from $_POSTS the user enters into the application. There is more code behind assigning the delimiters throughout the application than there is identifying the separate parts and running the query on the first, second third.
Hence the two delimiters are used.
 
Soldato
Joined
26 Dec 2003
Posts
16,522
Location
London
Bit shorter and a bit more extendable, if you want:

PHP:
<?php
$s = 'first&second:third';
preg_match('/(.*)&(.*):(.*)/', $s, $m);
list(, $first, $second, $third) = $m;
?>
 
Associate
Joined
19 Jun 2010
Posts
1,695
Location
Southampton City Centre
$m =

"If m is provided, then it is filled with the results of search. $m[0] will contain the text that matched the full pattern, $m[1] will have the text that matched the first captured parenthesized subpattern, and so on. "

Think of it as an array
 
Associate
Joined
19 Jun 2010
Posts
1,695
Location
Southampton City Centre
Code:
<?php

$str = 'foobar: 2008';

preg_match('/(?P<name>\w+): (?P<digit>\d+)/', $str, $matches);

/* This also works in PHP 5.2.2 (PCRE 7.0) and later, however 
 * the above form is recommended for backwards compatibility */
// preg_match('/(?<name>\w+): (?<digit>\d+)/', $str, $matches);

print_r($matches);

?> 

Would output

Array
(
    [0] => foobar: 2008
    [name] => foobar
    [1] => foobar
    [digit] => 2008
    [2] => 2008
)

from php.net
 
Associate
OP
Joined
4 Mar 2007
Posts
315
Location
United Kingdom
$m =

"If m is provided, then it is filled with the results of search. $m[0] will contain the text that matched the full pattern, $m[1] will have the text that matched the first captured parenthesized subpattern, and so on. "

Think of it as an array

Code:
<?php

$str = 'foobar: 2008';

preg_match('/(?P<name>\w+): (?P<digit>\d+)/', $str, $matches);

/* This also works in PHP 5.2.2 (PCRE 7.0) and later, however 
 * the above form is recommended for backwards compatibility */
// preg_match('/(?<name>\w+): (?<digit>\d+)/', $str, $matches);

print_r($matches);

?> 

Would output

Array
(
    [0] => foobar: 2008
    [name] => foobar
    [1] => foobar
    [digit] => 2008
    [2] => 2008
)

from php.net

Ahh, ok brilliant I will just dump the thing into a function and apply it throughout my site then.

Cheers all again for the helpful advice :)
 
Back
Top Bottom