只获取文本数字,alt,src和?从前100个字母。使用PHP

时间:2011-04-16 14:27:33

标签: php

示例我有一个$ text

<p>this is a link <a href="/link" class="u">a link</a> hello this is a text then a <img src="www.link.com/image.png">. then i have something like %,$,<?php etc ?> but i dont wanna lose my numbers and ? sign how can we do that?</p>;

并以某种方式

我需要它就像

  

这是一个链接你好,这是一个www.link.com/image.png。   文字然后一个。然后我有一些东西   喜欢等,但我不想失去我的   数字和?签署我们怎么能这样做?   (前100个字)

夏日

  • 以某种方式删除标签,图像(不知何故用src或alt改变它)
  • 删除$,#etc,除了'?'登录
  • 获得前100个单词
  • 得到数字

对于100个单词,我认为get a string before the first "." with php有一个很好的答案。我不太确定使用正则表达式还是?对于这一个。

感谢您的关注。

Adam Ramadhan

1 个答案:

答案 0 :(得分:1)

<?php
$text = '<p>this is a link <a href="/link" class="u">a link</a> hello this is a text then a <img src="www.link.com/image.png">. then i have something like %,$,etc but i dont wanna lose my numbers and ? sign how can we do that?</p>';
$text = preg_replace('/<a.*? href="(.*?)".*?>.*?<\/a>/i', 'http://link.com$1.', $text);
$text = strip_tags($text);
$text = str_replace(array('$,','#,', '%,'), array('', ''), $text);
preg_match('/^((?:\w*?\W){0,100})/', $text, $m);
$text = $m[1];
echo $text;

输出:

this is a link http://link.com/link. hello this is a text then a . then i have something like etc but i dont wanna lose my numbers and ? sign how can we do that?