Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

split a string

Status
Not open for further replies.

jianbo

Programmer
Dec 19, 2001
78
US
I tried to split a string with delimiter "|". Normally it works fine with explode, but if in this case: "|" is right near a number like "abc1990|na", I can't split it even with ascii code chr(124) instead of "|". How can I handle this situation?
Thanks.
 
I have used the pipe as delimiter in all different situations and have never run into a problem.

What does your code look like and what is the result?
 
Try this. DRJ.
$str = "1^0^0^1999|n/a^knee pain";
$tmp = explode("|",$str);
or $tmp = explode(chr(124),$str) //124 is ascii code of "|"
you can't get what you want
 
jianbo:

This script:

Code:
<?php
print '<pre>';

$a = 'abc1990|na';

$b = split ('\|', $a);

print_r ($b);

$b = explode ('|', $a);

print_r ($b);

$b = preg_split ('/\|/', $a);

print_r ($b);
?>

Produces the following output on my machine:

Code:
Array
(
    [0] => abc1990
    [1] => na
)
Array
(
    [0] => abc1990
    [1] => na
)
Array
(
    [0] => abc1990
    [1] => na
)

Want the best answers? Ask the best questions: TANSTAAFL!!
 
jianbo
The script you posted works fine for me.
I have no idea what it produces for you, but it works as expected.
 
Perhaps you have an old version of PHP? I'd suggest posting your output of
Code:
echo '<pre>';
print_r($tmp);

As well as your relevant system information... (OS, PHP Version)

-Rob
(and that's a me too on code executes fine)
 
All, Thanks.
Sorry, I find the problem is not explode. I put a condition before explode. I use strpos(&quot;|&quot;,$str)>0 to check the existaance of &quot;|&quot;, but it return false instead of a number.
Do you have idea about this?
Thanks again.
 
God bless me. Sorry to waste your time. I think I am too tired. How can I make such low level mistake!
 
Actually, it's a pretty understandable mistake. Most PHP functions similar to strpos() (explode(), split(), ereg(), preg_*()) take their input parameters in the order of &quot;needle&quot;, &quot;haystack&quot;. strpos() seems to be the weird one.

Want the best answers? Ask the best questions: TANSTAAFL!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top