php preg_replace,正则表达式

发布于 2024-09-01 08:40:19 字数 1272 浏览 4 评论 0原文

我正在尝试使用 php 和 preg_replace 从 yell.com 中提取邮政编码。 我成功提取了邮政编码,但只提取了地址。这是一个例子

$URL = "http://www.yell.com/ucs/UcsSearchAction.do?scrambleSeed=17824062&keywords=shop&layout=&companyName=&location=London&searchType=advance&broaderLocation=&clarifyIndex=0&clarifyOptions=CLOTHES+SHOPS|CLOTHES+SHOPS+-+LADIES|&ooa=&M=&ssm=1&lCOption32=RES|CLOTHES+SHOPS+-+LADIES&bandedclarifyResults=1";

//以字符串形式获取 yell.com 页面 $htmlContent = $baseClass->getContent($URL); //获取邮政编码和地址 $result2 = preg_match_all("/(.*)/", $htmlContent, $matches);

print_r($匹配);

上面的代码输出类似 Array ( [0] => Array ( [0] => 7, Royal Parade, Chislehurst, Kent BR7 6NR [1] => 55, Monmouth St, London, WC2H 9DG .... 我遇到的问题是我不知道如何只提取没有地址的邮政编码,因为它没有确切的位数(有时有 6 位数字,有时只有 5 次)基本上我应该提取最后的 2 个单词。来自每个数组。 预先感谢您的帮助!

I'm trying to extract the postal codes from yell.com using php and preg_replace.
I successfully extracted the postal code but only along with the address. Here is an example

$URL = "http://www.yell.com/ucs/UcsSearchAction.do?scrambleSeed=17824062&keywords=shop&layout=&companyName=&location=London&searchType=advance&broaderLocation=&clarifyIndex=0&clarifyOptions=CLOTHES+SHOPS|CLOTHES+SHOPS+-+LADIES|&ooa=&M=&ssm=1&lCOption32=RES|CLOTHES+SHOPS+-+LADIES&bandedclarifyResults=1";

//get yell.com page in a string $htmlContent = $baseClass->getContent($URL); //get postal code along with the address $result2 = preg_match_all("/(.*)</span>/", $htmlContent, $matches);

print_r($matches);

The above code ouputs something like
Array ( [0] => Array ( [0] => 7, Royal Parade, Chislehurst, Kent BR7 6NR [1] => 55, Monmouth St, London, WC2H 9DG .... the problem that I have is that I don't know how to extract only the postal code without the address because it doesn't have an exact number of digits (sometimes it has 6 digits and sometimes has only 5 times). Basically I should extract the lasted 2 words from each array .
Thank you in advance for any help !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

水晶透心 2024-09-08 08:40:19

快速&脏:

# your array item
$string = "7, Royal Parade, Chislehurst, Kent BR7 6NR";

# split on spaces
$bits = preg_split('/\s/', $string);

# last two bits
end($bits);
$postcode = prev($bits) . " " . end($bits);

echo $postcode;

查看它运行于:代码板

quick & dirty:

# your array item
$string = "7, Royal Parade, Chislehurst, Kent BR7 6NR";

# split on spaces
$bits = preg_split('/\s/', $string);

# last two bits
end($bits);
$postcode = prev($bits) . " " . end($bits);

echo $postcode;

See it run at: code pad

七颜 2024-09-08 08:40:19

如果您只需要匹配字符串中的最后两个单词,则可以使用此正则表达式:

\b\w+\s+\w+$

这将匹配它所说的内容:单词边界,一些非空单词,一些空格,然后是另一个单词,后面是字符串结尾锚。

<?php

$text = "7, Royal Parade, Chislehurst, Kent BR7 6NR";
$result =   preg_match("/\\b\\w+\\s+\\w+$/", $text, $matches);
print_r($matches);

?>

此打印

Array
(
    [0] => BR7 6NR
)

您还可以通过在最后一个单词后允许可选的尾随空格来使正则表达式更加健壮 \s* 等,但使用 $ 是主要思想。

If you just need to match the last two words in a string, you can use this regex:

\b\w+\s+\w+$

This will match what it says: a word boundary, some non-empty word, some white spaces, then another word, followed by end of string anchor.

<?php

$text = "7, Royal Parade, Chislehurst, Kent BR7 6NR";
$result =   preg_match("/\\b\\w+\\s+\\w+$/", $text, $matches);
print_r($matches);

?>

This prints:

Array
(
    [0] => BR7 6NR
)

You may also make the regex more robust by allowing optional trailing white spaces after the last word \s*, etc, but using the $ is the main idea.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文