Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP:regexp和特定标记剥离_Php_Regex_Preg Replace - Fatal编程技术网

PHP:regexp和特定标记剥离

PHP:regexp和特定标记剥离,php,regex,preg-replace,Php,Regex,Preg Replace,我正在寻找一种去除所有锚定标签的方法,我还希望删除从“,”到的所有内容,但应保持不变 脏输入: Abstractor HLTH<br> Account Representative, Major <a href="#P">P</a><br> Accountant <a href="#NP">NP</a>, <a href="#M">M</a>, <a href="#REA">REA<

我正在寻找一种去除所有锚定标签的方法,我还希望删除从“,”到
的所有内容,但

应保持不变

脏输入:

Abstractor HLTH<br>
Account Representative, Major <a href="#P">P</a><br>
Accountant <a href="#NP">NP</a>, <a href="#M">M</a>, <a href="#REA">REA</a>, <a href="#SKI">SKI</a><br>
Abstractor HLTH
客户代表,专业
会计,,
应该是这样的:

Abstractor HLTH<br>
Account Representative<br>
Accountant <br>
Abstractor HLTH
客户代表
会计
请帮忙

-- 以下是脏文本:

$str = sprintf('

Abstractor HLTH<br>
Account Representative, Major <a href="#P">P</a><br>

Accountant <a href="#NP">NP</a>, <a href="#M">M</a>, <a href="#REA">REA</a>, <a href="#SKI">SKI</a><br>
Accountant, Cost I & II (See Cost Accountant I, II) <a href="#FR">FR</a><br>
Accountant, General <a href="#G">G</a><br>
Accountant, General I (Junior) (See General Accountant) <a href="#FR">FR</a>, <a href="#O/G">O/G</a>, <a href="#W">W</a><br>

Accountant, General II (Intermediate) (See General Accountant) <a href="#FR">FR</a>, <a href="#O/G">O/G</a>, <a href="#W">W</a>, <a href="#HA">HA</a> <br>
Accountant, General III (Senior) (See General Accountant) <a href="#FR">FR</a>, <a href="#O/G">O/G</a>, <a href="#W">W</a> <br>

');
$str=sprintf('
抽象人HLTH
客户代表,专业
会计,,
成本一级和二级会计师(见成本一级和二级会计师)
总会计师
一级总会计师(初级)(见总会计师),
二级总会计师(中级)(见总会计师),,
三级总会计师(高级)(见总会计师),
');
用于标记,另一个用于标记。

我强烈建议使用HTML净化器

这是一个相当简单的设置,具有良好的声誉和极为强大的

是你的朋友。它有灵活的选择,而且非常复杂。使用str_replace或正则表达式执行此类操作是错误的;
$clean_string = strip_tags($original_string, '<br>');
这将除去br标签之外的所有内容

正如KingCrunch所说,
str_replace
strpos
替换其余部分。

有第二个参数,允许您提供一组允许的标记。它将去除除您提供的标签以外的所有标签:

$string = strip_tags($string, '<br>'); // will leave <br>-tags in place
$string=strip_标签($string,
);//将保留
-标记
通常使用正则表达式处理HTML字符串是不好的,但是假设所有链接都是这样形成的,那么使用
preg\u replace()
应该不会带来问题。试试这个

// Removes all links
$str = preg_replace("/<a href=\"#([A-Z\\/]+?)\">\\1<\\/a>(?:, )?/i", "", $str);

// Strip the comma and everything from the comma
// to the next <br> in the line
$str = preg_replace("/,(.*?)(?=<br>)/i", "", $str);
变成

Accountant NP

这并不是OP想要的。

第一行是:我正在寻找一种去除所有锚定标记的方法,我也希望删除从“,”到换行标记的所有内容,但换行符应该保留。没有使用HTML净化器,难道没有其他方法吗!试试我的答案,看看它是否适合你。注意:所有的行都是一个字符串。strip_标签只允许某些标签保留,但是OP想要相反的方式哦,是的,对。不管怎样,请留下答案,也许有人对它感兴趣。我没有投反对票,但为什么一个HTML净化器答案有一个向上票,而这个答案有一个向下票?它忽略了每一个br,直到最后,这是不想要的。。您的代码给出以下输出:Abstractor HLTH Account Representation忽略“accounter”,因为它介于“,”和最后一个之间br@ehmad1:你是说所有三行都在一个字符串中吗?@ehmad1:再给我一点时间。@ehmad1:你能把这个
sprintf()
部分粘贴到你的问题中吗?我不打算把这些都输入测试…@ehmad1:我也更新了我的代码。你能试试看你是否能得到想要的输出吗?
Accountant NP