简体   繁体   English

我在 PHP 上的 strpos 和 substr function 有问题

[英]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:我对 strpos 和 substr function 有问题,谢谢您的帮助:

$temp = "U:hhp|E:123@gmail.com,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??为什么output是这样的??

hhp+123@gmail.com,P:h123 +h123

but i want this:但我想要这个:

hhp+123@gmail.com,h123

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.问题是$find_and是 , 的索引,但是substr()的第三个参数需要是 substring 的长度,而不是结束索引。 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.对于$find_passeord你可以省略第三个参数,因为默认是字符串的结尾。

However, this would be simpler with a regular expression:但是,使用正则表达式会更简单:

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

if you have control over the input I would suggest如果您可以控制输入,我会建议

$temp = "U:hhp|E:123@gmail.com|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 https://3v4l.org/是一个很好的测试 php 代码的网站...这里是一个例子https://3v4l.org/

Echoing what Barmar just said in a comment, regular expressions are definitely the best way to "break up a string."与 Barmar 刚刚在评论中所说的相呼应,正则表达式绝对是“分解字符串”的最佳方式。 (It is quite-literally much of what they are for.) This is the preg_ family of PHP functions. (从字面上看,它们的用途很大。)这是 PHP 函数的preg_系列。 (eg preg_match , preg_match_all , preg_replace .) (例如preg_matchpreg_match_allpreg_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!"简而言之,您现在正在努力解决的所有strpos/substr逻辑...... “消失了!” 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."到目前为止,有成千上万的 web 页面讨论了这种“在一个单一的神秘字符串中的非凡‘编程语言’”。 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 .但它可能是任何从业者都知道最实用的技术,并且每种编程语言都以某种方式实现它,或多或少遵循(仍然活跃的)编程语言Perl首次设置的先例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM