Php 在从另一个网站中提取的数据上,使用文件“获取内容”(get)拆分标题()和“替换”(preg)

Php 在从另一个网站中提取的数据上,使用文件“获取内容”(get)拆分标题()和“替换”(preg),php,Php,我已使用file_get_contents()从另一个网站提取数据 这是源代码的一部分: <font style="font-size:10px;color:#123333;font-weight:BOLD;">1,22 €</font> 1,22€ 我使用split_on_title函数从字符串中提取1,22欧元: $split_on_title = preg_split("<font style=\"font-size:10px;color:#123333;

我已使用file_get_contents()从另一个网站提取数据

这是源代码的一部分:

<font style="font-size:10px;color:#123333;font-weight:BOLD;">1,22 €</font>
1,22€
我使用split_on_title函数从字符串中提取1,22欧元:

$split_on_title = preg_split("<font style=\"font-size:10px;color:#123333;font-weight:BOLD;\">", $source);
$split_on_endtitle = preg_split("</font>", $split_on_title[1]);
$title = $split_on_endtitle[0];
preg_replace('> €<', '', $title);
$split\u on\u title=preg\u split(“,$source”);
$split_on_endtitle=preg_split(“,$split_on_title[1]);
$title=$split_on_endtitle[0];
当我回显$title时,firefox返回:

>1,22 €<
>1,22<
我在字符串上使用了preg_replace:

$split_on_title = preg_split("<font style=\"font-size:10px;color:#123333;font-weight:BOLD;\">", $source);
$split_on_endtitle = preg_split("</font>", $split_on_title[1]);
$title = $split_on_endtitle[0];
preg_replace('> €<', '', $title);
preg_replace('>

如何提取1,22欧元的净价值?至少只有1,22欧元。 提前谢谢

编辑:

我明白我提供的数据很难,我会写一份更大的数据

<tr>
    <td width="80" align="left" valign="top">
        <b> Price:</b>
    </td>
    <td align="left"  valign="top">
        <font style="font-size:10px;color:#123333;font-weight:BOLD;">1,22 €</font>
    </td>
</tr>

价格:
1,22 €

我需要帮助从这个来源获得1,22欧元。

请在html页面的
部分添加对UTF-8的必要支持

缺少该符号,因此未正确呈现欧元符号

有关如何放入此和其他元标记的更多详细信息:

为什么不使用
preg\u match
并抓住字体标签之间的所有内容

$re = "/<font.*>(.*)<\\/font>/i"; 
$str = "<font style=\"font-size:10px;color:#123333;font-weight:BOLD;\">1,22 €</font>"; 

preg_match($re, $str, $matches);
echo $matches[1];
$re=“/(.*))/i”;
$str=“1,22欧元”;
预匹配($re,$str,$matches);
echo$匹配[1];
下面是模式的分解方式

<font matches the characters <font literally (case insensitive)
.* matches any character (except newline)
Quantifier: * Between zero and unlimited times, as many times as possible, giving back as needed [greedy]
> matches the characters > literally
1st Capturing group (.*)
.* matches any character (except newline)
Quantifier: * Between zero and unlimited times, as many times as possible, giving back as needed [greedy]
< matches the characters < literally
\/ matches the character / literally
font> matches the characters font> literally (case insensitive)
i modifier: insensitive. Case insensitive match (ignores case of [a-zA-Z])
字面意思
第一个捕获组(*)
*匹配任何字符(换行符除外)
量词:*在零次和无限次之间,尽可能多地,根据需要回馈[贪婪]
<按字面意思匹配字符<
\/与字符/字面匹配
font>按字面意思匹配字符font>(不区分大小写)
i修饰符:不区分大小写。不区分大小写匹配(忽略[a-zA-Z]的大小写)

关于preg_match fix,@pavlovich的答案给了我一个>1,22欧元的输出,这是一个很好的建议,但是输出现在>1,22欧元(抱歉,我在前面的评论中写了另一个值),我认为@slapyo在他的答案中做得很正确;)我尝试了硬编码(你写的代码),遇到了这个错误:注意:数组到字符串的转换。。。我该拿它怎么办$matches@OzanAtmar像
echo$matches[1]一样输出它@pavlovich谢谢。。。我更新了这个示例,以反映如何输出值。实际上,当您在$str变量中写入该字符串时,它会起作用。但这是一个充满html标记的页面,前面的标记是“”。在代码中重复多次。因此,使用preg_split()从源代码中提取“1,22€”是不可能的(或者我不知道)。如果这是唯一一个具有此样式的特定字体标记的地方,那么您可以这样做
$re=“/.*(.*)。*/is”。但在不了解更多信息的情况下,这不是一种非常理想的方法。