English 中文(简体)
• 如何在PHP中有效地找到一个具体的子体?
原标题:How to efficiently find a specific substring in PHP?

在大韩民国,我知道如何做到这一点,但我很想知道解决这一问题的更好/更可取的解决办法(如果有的话)。

I:

Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; GTB6.6; Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) ; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)

我很想在这一扼杀中找到最高的MSIE版本(即7)。

目前,我使用探照和预言,用两倍时间,加上插手,然后在校对和比较插手。 它运作良好,但远非立法者。 任何关于如何改进这种想法?

铭记:

  • The string can have any number of MSIE occurrences (or even none)
  • MSIE is case insensitive
  • MSIE is followed by a white space (space, tab...) then by the version. The version can contain number letters and dots. The version string is ended by either a white space, a ; or the end of the line.

感谢!

最佳回答

Try:

<?php
$str =  Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; GTB6.6; Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1), MSIE 8.7abc; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729) MSIE 9.0 ;

if (preg_match_all( /msies*([^s;$]+)/i , $str, $m)) {
    usort($m[1],  version_compare );
    $max = array_pop($m[1]);

    echo $max;
}
问题回答

如果你试图实施浏览器检测,。 它比本复杂得多。

也许已经为你做了这项工作。

http://php.net/manual/en/Function.get-browser.php rel=“nofollow”get_browser ( function, 以及相关的browsecap.ini文档。

然而,我一般认为,检测浏览器是一种坏的想法。 这根本不可靠,因为浏览器能够打碎其用户代理人。 即便浏览器没有,也仍然可以通过近似和安全软件加以改变或空白。

与此类似:

$str = "Mozilla/4.0 (compatible; msie 7.0; Windows NT 5.1; Trident/4.0; GTB6.6; Mozilla/4.0 (compatible; MSIE 7.1; Windows NT 5.1; SV1) ; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)";

preg_match_all( /MSIEs([d.]+);/i , $str, $matches);

if(!empty($matches[1])){
    echo "Max version found:" . max($matches[1]);
}else{
    echo "None found";
}

斜线确实是你描述的。 我不敢肯定,它能够取得比在PHP更高的效率。

Returns the numeric position of the first occurrence of needle in the haystack string. Unlike strpos(), stripos() is case-insensitive.





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签