今天应老板的需求,需要将不是我们的页面修改一个链接,用js+iframe应该也能实现,但是我想尝试一下php实现方法。

  首先你得先把别人的页面download到你的php中,实现方法可以用curl, file,这里有一篇文章写的不错http://www.11jn.com/phpbb/viewtopic.php?f=31&t=1390,这里就不多说。

  然后就是用正则表达式找到你的链接,因为是具体的链接,就直接写了,比如百度 (http\:\/\/www.baidu.com)

下面就是主要函数 preg_replace()

mixed preg_replace ( mixed $pattern ,     mixed $replacement ,   mixed $subject     [, int $limit = - [, int &$count ]] )

  正则表达式(要被替换的)      替换的内容                     需要匹配替换的对象       可选,指定替换的个数,如果省略 limit 或者其值为 -1,则所有的匹配项都会被替换

  替换一个的实例:

        要把www.baidu.com替换成www.google.com

$content='http://www.baidu.com';

$replace=preg_replace("(http\:\/\/www.baidu.com)","http://www.google.com",$content);
echo $replace;

  替换的第二个实例:

      在一个段落中替换两个多个字段,www.baidu.com替换成www.google.com ,并且Windows替换成Linux

$content="http://www.baidu.com on Windows.";
$str1=array("(http\:\/\/www.baidu.com)","(Windows)");
$str2=array("http://www.google.com","Linux");
$replace=preg_replace($str1,$str2,$content);
echo $replace;

 

PHP中正则替换函数preg_replace用法笔记的更多相关文章

  1. PHP模板引擎正则替换函数 preg_replace 与 preg_replace_callback 使用总结

    在编写PHP模板引擎工具类时,以前常用的一个正则替换函数为 preg_replace(),加上正则修饰符 /e,就能够执行强大的回调函数,实现模板引擎编译(其实就是字符串替换). 详情介绍参考博文:P ...

  2. PHP 字符串正则替换函数preg_replace使用说明

    1. preg_replace() $msg = preg_replace("/<style>.+<\/style>/is", "", ...

  3. php正则替换函数-----preg_replace ( mixed $pattern , mixed $replacement , mixed $subject [, int $limit = -1 [, int &$count ]] )

    preg_replace — 执行一个正则表达式的搜索和替换 说明 mixed preg_replace ( mixed $pattern , mixed $replacement , mixed $ ...

  4. C语言中关于scanf函数的用法

    scanf()函数的控制串 函数名: scanf 功 能: 执行格式化输入 用 法: int scanf(char *format[,argument,...]); scanf()函数是通用终端格式化 ...

  5. JavaScript中字符串分割函数split用法实例

    这篇文章主要介绍了JavaScript中字符串分割函数split用法,实例分析了javascript中split函数操作字符串的技巧,非常具有实用价值,需要的朋友可以参考下 本文实例讲述了JavaSc ...

  6. C中的时间函数的用法

    C中的时间函数的用法    这个类展示了C语言中的时间函数的常用的用法. 源代码: #include <ctime>#include <iostream> using name ...

  7. (转)Python中的split()函数的用法

    Python中的split()函数的用法 原文:https://www.cnblogs.com/hjhsysu/p/5700347.html Python中有split()和os.path.split ...

  8. 数据库SQL中case when函数的用法

    Case具有两种格式,简单Case函数和Case搜索函数.这两种方式,可以实现相同的功能.简单Case函数的写法相对比较简洁,但是和Case搜索函数相比,功能方面会有些限制,比如写判断式. 简单Cas ...

  9. Oracle的字符替换函数translate用法

    参考文档如下:http://www.banping.com/2009/05/18/oracle_function_translate/ Oracle提供了一个字符替换函数translate,不同于re ...

随机推荐

  1. poj 3134 Power Calculus(IDA*)

    题目大意: 用最小的步数算出  x^n 思路: 直接枚举有限步数可以出现的所有情况. 然后加一个A*   就是如果这个数一直平方  所需要的步骤数都不能达到最优   就剪掉 #include < ...

  2. 【转】2D动画:view的Matrix

    Matrix,中文里叫矩阵,高等数学里有介绍,在图像处理方面,主要是用于平面的缩放.平移.旋转等操作. 首先介绍一下矩阵运算.加法和减法就不用说了,太简单了,对应位相加就好.图像处理,主要用到的是乘法 ...

  3. C#自定义控件背景色透明的方法

    I struggled for ages with the problem of having controls show through a control that was painted on ...

  4. php中curl不支持https的解决办法

    在php程序中使用curl去访问https站点时,报错:Protocol https not supported or disabled in libcurl 该错误信息表示php当时编译时使用的cu ...

  5. win8 企业版 安装 .net2.0 .net 3.5

    Windows 8 默认集成 .Net Framework 4.5,因此运行一些基于3.5或以前版本的程序时会弹出这个提示. 2012-3-2 15:24 上传 下载附件 (23.91 KB)   这 ...

  6. MySQL 多会话之间更新数据的小实例

    1:创建一个实验表 mysql> use test; mysql> CREATE TABLE t -> (id int(11) NOT NULL DEFAULT 0, -> n ...

  7. MySQL 4种日志

  8. Codeforces Gym 100015H Hidden Code 暴力

    Hidden Code 题目连接: http://codeforces.com/gym/100015/attachments Description It's time to put your hac ...

  9. Codeforces Gym 100015A Another Rock-Paper-Scissors Problem 找规律

    Another Rock-Paper-Scissors Problem 题目连接: http://codeforces.com/gym/100015/attachments Description S ...

  10. [Express] Level 2: Middleware -- 2

    Logging Middleware Help finish the following middleware code in the logger.js file: On the response  ...