简单的php strpos没有找到价值

时间:2017-03-03 12:32:46

标签: php

我试图找到这样的子字符串:

// get needle for strpos
$someStringContainingHtmlTags = '<table><td class="size1of2 bold">Order no</td></div><td class="size1of2" data-template="productBestnr">64210</td></table>';

$re = '/<td class="size1of2" data-template="productBestnr">\d+<\/td>/';
preg_match($re, $someStringContainingHtmlTags , $matches);

$art = (string)$matches[0];

$needle = '<td class="size1of2" data-template="productBestnr">'.$art.'</td>';

// echos nothing
echo strpos($someStringContainingHtmlTags , $needle);

如果我将$ art替换为实际值64210strpos可以正常工作。

感谢您的帮助!

3 个答案:

答案 0 :(得分:1)

为了让您的$art获得正确的值(64210),您应该将\d放入一个组:$re = '/<td class="size1of2" data-template="productBestnr">(\d+)<\/td>/';

答案 1 :(得分:0)

检查一下,

$someStringContainingHtmlTags = '<table><td class="size1of2 bold">Order no</td></div><td class="size1of2" data-template="productBestnr">64210</td></table>';
$re = '/<td class="size1of2" data-template="productBestnr">\d+<\/td>/';
preg_match($re, $someStringContainingHtmlTags , $matches);
$art = (string)$matches[0];
// echos nothing
echo strpos($someStringContainingHtmlTags , $art);

你的美术实际上是你的针。而你正在针内加入niddle。

一旦回应$ art,你就会得到它。

答案 2 :(得分:0)

正如@Sir McPotato告诉你需要将表达放入群组中。 matches的第二个元素将返回64210

<?php
// get needle for strpos
$someStringContainingHtmlTags  = '<table><td class="size1of2 bold">Order no</td></div><td class="size1of2" data-template="productBestnr">64210</td></table>';

$re = '/<td class="size1of2" data-template="productBestnr">(\d+)<\/td>/';
preg_match($re, $someStringContainingHtmlTags , $matches);

$art = (string)$matches[1];

$needle = '<td class="size1of2" data-template="productBestnr">'.$art.'</td>';

// echos nothing
echo strpos($someStringContainingHtmlTags , $needle);