Home  >  Article  >  Backend Development  >  php string matching

php string matching

WBOY
WBOYOriginal
2016-08-04 09:19:26959browse

There are two arrays, namely

<code>$template1 = array(
            "您好,你的网址是@,请访问",
            "您预约的时间在@,请按时到@报道",
            "您的面试一结束,请给@评价吧,网址在@"
)
$template2 = array(
            "你的验证码是@",
            "所有的特殊字符有@"
)
</code>

The @ is any possible string, now if I get a string

<code>$message = "您好,你的网址是http://www.baidu.com/jsdkj,请访问";</code>

How to determine which array this $message belongs to?

Reply content:

There are two arrays, namely

<code>$template1 = array(
            "您好,你的网址是@,请访问",
            "您预约的时间在@,请按时到@报道",
            "您的面试一结束,请给@评价吧,网址在@"
)
$template2 = array(
            "你的验证码是@",
            "所有的特殊字符有@"
)
</code>

The @ is any possible string, now if I get a string

<code>$message = "您好,你的网址是http://www.baidu.com/jsdkj,请访问";</code>

How to determine which array this $message belongs to?

Convert the string in the template into a regular expression, and then match the messages one by one.

For example:

<code>$template1 = array(
            "您好,你的网址是.*,请访问",
            "您预约的时间在.*,请按时到.*报道",
            "您的面试一结束,请给.*评价吧,网址在.*"
)
</code>

It is worth noting: If the template array is relatively large, it may affect performance. Also, template rendering is usually supposed to be one-way, so I doubt the legitimacy of this requirement.

Can’t it be done with just one in_array? Could it be that I misunderstood what the poster meant?

Just judge strpos in the form of keywords

This kind of logic is too rare. Usually, the message is obtained based on variable + template, and you do not need to infer based on the message. Is there something wrong in your understanding?

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn