Forum Moderators: coopster

Message Too Old, No Replies

Splitting a string into it's parts

Is there another function other than "explode?"

         

calvinmicklefinger

5:59 pm on Jan 27, 2005 (gmt 0)

10+ Year Member



I have a string, let's say "qx1234," which I would like to split into two parts, "qx" and "1234."

Is there a funtion other than "explode" that performs this task? Maybe something such as "Left" or "right" functions? If I need to use "explode," how would I do that?

Many thanks.

jusdrum

6:31 pm on Jan 27, 2005 (gmt 0)

10+ Year Member



Try using preg_match:

Example:

$String = "qx1234";
preg_match("/([a-zA-Z]+)([0-9]+)/",$String,$Match);
print_r($Match);

Output:

Array
(
[0] => qx1234
[1] => qx
[2] => 1234
)

[0] is the whole matching string, and the subsequent array values are the captured parts of the string.

Of course you will have to modify the regex to suit your needs, but this particular example will work for the string you supplied.

Hope this helps!

jollymcfats

6:37 pm on Jan 27, 2005 (gmt 0)

10+ Year Member



You can also use substr() [php.net] much like 'left()' and 'right()' functions.

lZakl

6:54 pm on Jan 27, 2005 (gmt 0)

10+ Year Member



<?
$vari = "qx1234";

$front = substr($vari, 0, 2);
$back = substr($vari, -4);

echo "$front<br>$back";
?>

<ADDED> NOT TESTED! lol </ADDED>

calvinmicklefinger

7:49 pm on Jan 27, 2005 (gmt 0)

10+ Year Member



Many thanks,

The substr() seems to fit my needs best. I've used it in a file called ref.php as follows (note that there are ten lines in this file) ...

<?php
if (substr($CurrentPage, 0, 2) === "qx") {
$refnum = substr($CurrentPage, -4);
switch ($refnum):
case 00001: header("Location: [msn.com");...]
case 00002: header("Location: [google.com");...]
default: header("Location: [example.com...] ;
endswitch;
}
?>

I include this file in the code before the <HTML> tag in my file called "content.php" as in the following extract ...

$URL_data = explode("/", getenv("REQUEST_URI"));
$_SESSION['CurrentPage'] = $URL_data[2] ;

// Redirect test of ref.php shown above
include '/var/www/html/php/ref.php';

// Include page sought if available
if(file_exists('/var/www/html/php/' . $CurrentPage . '.php'))
{include '/var/www/html/php/' . $CurrentPage . '.php' ;}
else
{include '/var/www/html/php/main.php'; $CurrentPage='main';}
?>
<html>

I am getting a parser error that says there is an unexpecte "$" in line 11 of ref.php. Then the HTML loads as I would expect since the URL_data[]2] did not include the "qx." Since I only have 10 lines in ref.php, I am confused.

Cam amyone explain or help?

Many thanks.

[edited by: jatar_k at 8:58 pm (utc) on Jan. 27, 2005]

dreamcatcher

7:56 pm on Jan 27, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Last line errors are usually a missing brace. Have you checked your braces to see if there is a match?

calvinmicklefinger

11:41 pm on Jan 27, 2005 (gmt 0)

10+ Year Member



It appears to have been a "trickle down" from outside (before) the include directive for this file.

I added a semicolon after the last curly bracket at the end of an "if" statement and the parser error did not return an error.

if ($LandingPage == "") { $_SESSION['LandingPage'] = $URL_data[2] ; } ; <-- the semicolon I added

Only problem is, now my logic doesn't work and I don't get content included. Oh me, oh my.

Many thanks for the guidance.

Birdman

12:07 am on Jan 28, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Your switch() syntax is incorrect, I believe.

It should be:

switch ($refnum){
case 00001: header("Location: somesite");
break;
case 00002: header("Location: anothersite");
break;
default: header("Location: yoursite);
}