0

我有一个包含这些长描述的数据库,我需要弄清楚如何从描述中提取位置代码。我用preg_replace()它来匹配它,就像这个示例条目一样。

$string = "Honda 1982 VF750C Magna right-side radiator trim panel. Good, damage-free condition. Needs cut and polished. Cheap, fast shipping! 011425 H6 <img src=\">http://www.roofis27.com/motorcycle/10_01_14/030.JPG\"> n=\">";
$pattern = '(\d\d\d\d\d\d\s\D\d)'; 
$replace = '$1';
echo 'Replaced String: ' . preg_replace($pattern, $replace, $string) . '<br>';
echo '<br>';
echo 'Original String: ' . $string;

我需要做的是删除011425 H6. $string我不知道如何摆脱其余的字符串。我可以匹配模式,但是我用什么正则表达式来删除字符串的其余部分?更少删除之前和之后的所有内容011425 H6。任何帮助将不胜感激,并提前感谢:)

4

1 回答 1

0

只是一个小的修改,然后它应该工作:

<?php

$string = "Honda 1982 VF750C Magna right-side radiator trim panel. Good, damage-free condition. Needs cut and polished. Cheap, fast shipping! 011425 H6 <img src=\">http://www.roofis27.com/motorcycle/10_01_14/030.JPG\"> n=\">";
$pattern = '/^.*\b(\d\d\d\d\d\d\s\D\d)\b.*$/';
$replace = '$1';
echo 'Replaced String: ' . preg_replace($pattern, $replace, $string) . '<br>';
echo '<br>';
echo 'Original String: ' . $string;
于 2013-09-04T00:02:37.340 回答