0

我不太了解正则表达式。并且需要帮助从字符串中获取一些数据,如果有人可以提供帮助,请。当我在这里粘贴字符串时,它会格式化 HTML,所以请检查 pastebin 链接。

好的,所以我需要的是这个字符串中的每个字段数据,即“Pickup From:”,“Deliver To:”,“Special Instruction:”等。格式将保持不变,即每次中断都会出现,等等。只有内部数据会改变。

例如:取件地点:美国华盛顿特区

所以我只需要回答“美国华盛顿特区”。

检查 Pastebin

任何人都可以帮忙吗?我正在使用 php。

4

4 回答 4

1

我认为您在这里不需要正则表达式。使用explode().

$string = 'Pickup From: Washington, DC, USA';
$ar = explode(': ', $string);
if (count($ar) == 2) { # Want to check to make sure it's not empty!
   echo $ar[1]; # Echoes "Washington, DC, USA"
}
于 2013-11-06T14:49:00.857 回答
0

您可能需要所有字段:

$in = '<a href="example.com/edit"> Edit Your Delivery Details  </a>Pickup From: Washington, DC, USA<br>Deliver To: WDG Architecture, Connecticut Avenue Northwest, Washington, D.C., DC<br>Special Instruction: Yes, order fast<br>Person Name: sdf<br>Person Contact: dfsdf<br>Person Email: sdfsdasd@asd.asd<br>Pickup Day: Today<br>Pickup Time: ASAP<br><br>';

$lines = explode("<br>", $in);
$matches = array();
foreach ($lines as $line) {
    if (strstr($line, ":")) {
        $matches[] = preg_split("/\s*:\s*/", $line, 2);
    }
}
print_r($matches);

给出:

Array
(
    [0] => Array
        (
            [0] =>  Edit Your Delivery Details  
Pickup From
            [1] => Washington, DC, USA
        )

    [1] => Array
        (
            [0] => Deliver To
            [1] => WDG Architecture, Connecticut Avenue Northwest, Washington, D.C., DC
        )

    [2] => Array
        (
            [0] => Special Instruction
            [1] => Yes, order fast
        )

    [3] => Array
        (
            [0] => Person Name
            [1] => sdf
        )

    [4] => Array
        (
            [0] => Person Contact
            [1] => dfsdf
        )

    [5] => Array
        (
            [0] => Person Email
            [1] => sdfsdasd@asd.asd
        )

    [6] => Array
        (
            [0] => Pickup Day
            [1] => Today
        )

    [7] => Array
        (
            [0] => Pickup Time
            [1] => ASAP
        )

)

正如你所看到的,它还不是很完美,但它能让你到达那里。

使用 strpos 查找</a>和修剪。如果你有更多这样的噪音,你也需要修剪它。


对投反对票的人:你能解释一下你的投反对票吗?我错过了什么?

于 2013-11-06T14:51:37.020 回答
0

考虑以下正则表达式...

((?<=(Pickup\sFrom:)).*|(?<=(Deliver\sTo:)).*|(?<=(Special\sInstruction:)).*)
于 2013-11-06T14:57:57.077 回答
-1

可以不用正则表达式来判断:

$test = "Pickup From: Washington, DC, USA";
$position = strrpos($test, ':');
$string = $test;
if ($position !== false)
    $string = substr($test, $position+1);
else
    $string = $test;

echo $string;

用正则表达式:

$test = "Pickup From: Washington, DC, USA";
if (preg_match('/: ([^,]+, [A-Z]{2}, [A-Z]{3})$/', $test, $m) !== false)
    $string = $m[1];
else
    $string = $test;

print_r($string);
于 2013-11-06T14:56:09.927 回答