【已解决】php正则表达式问题

php中文网
发布: 2016-06-06 20:29:52
原创
1429人浏览过

【已解决】

<code>echo '<meta charset=utf8>';
$pa = '%<p>(.*?)</p>%sim';
preg_match($pa, trim($message), $arr);
if(strpos("Welcome back",$message)){
    preg_match('/.\d{1,}.\d{1,2}/', $arr[1], $arr);
}else{
    preg_match('/.\d{1,}.\d{1,2}/', $arr[1], $arr);
}
echo($arr[0]);</code>
登录后复制

===================================================================

多语言网站,需要提取9.00,其中$不确定,因为有可能是¥,£等符号,

PHP轻论坛
PHP轻论坛

简介PHP轻论坛是一个简单易用的PHP论坛程序,适合小型社区和个人网站使用。v3.0版本是完全重构的版本,解决了之前版本中的所有已知问题,特别是MySQL保留字冲突问题。主要特点• 简单易用:简洁的界面,易于安装和使用• 响应式设计:适配各种设备,包括手机和平板• 安全可靠:避免使用MySQL保留字,防止SQL注入• 功能完善:支持分类、主题、回复、用户管理等基本功能• 易于扩展:模块化设计,便于

PHP轻论坛 21
查看详情 PHP轻论坛

可以确定的是$299.00前都包含一个空格,小数点后都有两位数,

立即学习PHP免费学习笔记(深入)”;

换句话说,需要从:

<code>您的账户有未付账单金额 $299.00,请尽快完成付款。</code>
登录后复制

或者

<code>Your account has an outstanding balance of $299.00. Please make a payment at your earliest convenience.</code>
登录后复制

提取到$299.00。

中文显示:

<code><div class="col-md-12"><div class="alert alert-warning alert-dismissable">
        <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
        <strong>欢迎回来, 陶!</strong>
                <p>您的账户有未付账单金额 $299.00,请尽快完成付款。</p>
                <p><a href="/client/pay/index/USD/" class="btn btn-warning"><i class="fa fa-exclamation-triangle fa-fw"></i> 进行付款</a></p>
    </div>
</div></code>
登录后复制
<code><div class="alert alert-warning alert-dismissable">
        <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
        <strong>Welcome back, 陶!</strong>
                <p>Your account has an outstanding balance of $299.00. Please make a payment at your earliest convenience.</p>
                <p><a href="/client/pay/index/USD/" class="btn btn-warning"><i class="fa fa-exclamation-triangle fa-fw"></i> Make Payment</a></p>
    </div></code>
登录后复制

回复内容:

【已解决】

<code>echo '<meta charset=utf8>';
$pa = '%<p>(.*?)</p>%sim';
preg_match($pa, trim($message), $arr);
if(strpos("Welcome back",$message)){
    preg_match('/.\d{1,}.\d{1,2}/', $arr[1], $arr);
}else{
    preg_match('/.\d{1,}.\d{1,2}/', $arr[1], $arr);
}
echo($arr[0]);</code>
登录后复制

===================================================================

多语言网站,需要提取$299.00,其中$不确定,因为有可能是¥,£等符号,

可以确定的是$299.00前都包含一个空格,小数点后都有两位数,

立即学习PHP免费学习笔记(深入)”;

换句话说,需要从:

<code>您的账户有未付账单金额 $299.00,请尽快完成付款。</code>
登录后复制

或者

<code>Your account has an outstanding balance of $299.00. Please make a payment at your earliest convenience.</code>
登录后复制

提取到$299.00。

中文显示:

<code><div class="col-md-12"><div class="alert alert-warning alert-dismissable">
        <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
        <strong>欢迎回来, 陶!</strong>
                <p>您的账户有未付账单金额 $299.00,请尽快完成付款。</p>
                <p><a href="/client/pay/index/USD/" class="btn btn-warning"><i class="fa fa-exclamation-triangle fa-fw"></i> 进行付款</a></p>
    </div>
</div></code>
登录后复制
<code><div class="alert alert-warning alert-dismissable">
        <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
        <strong>Welcome back, 陶!</strong>
                <p>Your account has an outstanding balance of $299.00. Please make a payment at your earliest convenience.</p>
                <p><a href="/client/pay/index/USD/" class="btn btn-warning"><i class="fa fa-exclamation-triangle fa-fw"></i> Make Payment</a></p>
    </div></code>
登录后复制

从代码来看,这段代码的内容除了名称和金额之外其他的应该是不变的内容,这样的话就比较好办了。
直接去匹配金额/\s(.\d+\.\d{2})/g就是了,前提是针对该段代码,如果是整个页面的话,需要把匹配局限到这段代码中来。。

已解决:

<code>echo '<meta charset=utf8>';
$pa = '%<p>(.*?)</p>%sim';
preg_match($pa, trim($message), $arr);
if(strpos("Welcome back",$message)){
    preg_match('/.\d{1,}.\d{1,2}/', $arr[1], $arr);
}else{
    preg_match('/.\d{1,}.\d{1,2}/', $arr[1], $arr);
}
echo($arr[0]);</code>
登录后复制

不知道题主的目的,提取这个金额做何用,是要去进行支付吗?如果是支付,这个金额不应该去服务端验证吗?毕竟这个价格前端可以篡改。

如果只是仅仅需要匹配的话,可以使用这个表达式

<code>$pattern = '#([\$|¥|£][1-9][0-9]*.[0-9]{2})#';
// $subject = '您的账户有未付账单金额 $299.00,请尽快完成付款。';
$subject = 'Your account has an outstanding balance of $299.00. Please make a payment at your earliest convenience.';
preg_match($pattern, $subject, $a);
var_dump($a);
// 输出:
array(2) {
  [0] =>
  string(7) "$299.00"
  [1] =>
  string(7) "$299.00"
}</code>
登录后复制

汗颜,不知道为什么变成了两个?原因不明

相关标签:
PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号