How to replace a certain value in a link in php

藏色散人
Release: 2023-03-14 20:54:02
Original
1871 people have browsed it

php method to replace a certain value in the link: 1. Create a PHP sample file; 2. Replace the href attribute value in the a tag through the "function a_replace_href($str) {...}" method. Can.

How to replace a certain value in a link in php

The operating environment of this article: Windows7 system, PHP7.1 version, DELL G3 computer

How to replace the link in php a certain value?

php Replace the href attribute of the a tag in html

The data collected when imitating the site contains many a links that can jump to other sites, which can be replaced during collection Drop

<?php
// 替换a标签中的href属性值
function a_replace_href($str) {
  $preg = &#39;/href=(\"|\&#39;)(.*?)(\"|\&#39;)/i&#39;;
  $replacestr = &#39;href="#"&#39;;
  $res = preg_replace($preg, $replacestr, $str);
  return $res;
}
// 获取字符串中的所有a标签
function from_str_get_a($str){
    $preg = &#39;/<a .*?>/i&#39;;
    preg_match_all($preg, $str, $matchs);
    return $matchs[0];
}
$str = file_get_contents("index.html");
$data = from_str_get_a($str);//获取字符串中的a标签
$data1 = a_replace_href($data);//替换后应该显示的a标签
$res = $str;
// 反复对字符串进行查找替换
foreach ($data as $key => $value) {
    $res = str_replace($value,$data1[$key],$res,$i);
}
// 另存文件
file_put_contents("index1.html",$res);
Copy after login

Recommended study: "PHP Video Tutorial"

The above is the detailed content of How to replace a certain value in a link in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template