hhp
hhp

Reputation: 3

I have a problem with the strpos & substr function on PHP

I have a problem with the strpos & substr function, thank you for your help:

$temp = "U:hhp|E:[email protected],P:h123";

$find_or = strpos($temp,"|");
$find_and = strpos($temp,",");

$find_user = substr($temp,2,$find_or-2);
$find_email = substr($temp,$find_or+3,$find_and);
$find_passeord = substr($temp,$find_and+3,strlen($temp));

echo("$find_user+$find_email+$find_passeord<br/>");

/************************************/

Why is the output like this ??

[email protected],P:h123 +h123

but i want this:

[email protected],h123

Upvotes: 0

Views: 317

Answers (3)

Mike Robinson
Mike Robinson

Reputation: 8945

Echoing what Barmar just said in a comment, regular expressions are definitely the best way to "break up a string." (It is quite-literally much of what they are for.) This is the preg_ family of PHP functions. (e.g. preg_match, preg_match_all, preg_replace.)

The million-dollar idea behind a "regular expression" is that it is a string-matching pattern. If the string "matches" that pattern, you can easily extract the exact substrings which matched portions of it.

In short, all of the strpos/substr logic that you are right now wrestling with ... "goes away!" Poof.

For example, this pattern: ^(.*)|(.*),(.*)$ ...

It says: "Anchored at the beginning of the string ^, capture () a pattern consisting of "zero or more occurrences of any character (.*), until you encounter a literal |. Now, for the second group, proceed until you find a ,. Then, for the third group, proceed to take any character until the end of the string $."

You can "match" that regular expression and simply be handed all three of these groups! (As well as "the total string that matched.") And you didn't have to "write" a thing!

There are thousands of web pages by now which discuss this "remarkable 'programming language' within a single cryptic string." But it just might be the most pragmatically-useful technology for any practitioner to know, and every programming language somehow implements it, more-or-less following the precedent first set by the (still active) programming language, Perl.

Upvotes: 0

Barmar
Barmar

Reputation: 781068

The problem is that $find_and is the index of ,, but the third argument to substr() needs to be the length of the substring, not the ending index. So

$find_email = substr($temp,$find_or+3,$find_and);

should be

$find_email = substr($temp,$find_or+3,$find_and-$find_or-3);

For $find_passeord you can omit the 3rd argument, since the default is the end of the string.

However, this would be simpler with a regular expression:

if (preg_match('/^U:(.*?)\|E:(.*?),P:(.*)/', $temp, $match)) {
    list($whole, $user, $email, $password) = $match;
}

Upvotes: 2

Ryan H
Ryan H

Reputation: 410

if you have control over the input I would suggest

$temp = "U:hhp|E:[email protected]|P:h123";
list($user, $email, $password) = explode("|",$temp);
$user = explode(":",$user)[1];
$email = explode(":",$email)[1];
$password = explode(":",$password)[1];

if not then I still recommend exploding the string into parts and work your way down to what you need . https://3v4l.org/ is a great site for testing php code ... here is an example of this working https://3v4l.org/upEGG

Upvotes: 0

Related Questions