1

I have a content like

<p>
<strong><span style="font-family: monospace; white-space: pre-wrap; ">Description:Getting Started contains a list of tasks you might want to perform when you set up your computer. Tasks in Getting Started include: &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;  Dnescription:Getting Started contains a list of tasks you might want to perform when you set up your computer. Tasks in Getting Started include:</span></strong></p>

Now i want to select only 50 characters i.e only

Description:Getting Started contains a list of tasks you might want to perform when you set up your computer. Tasks in Getting Started include:

it should exclude tags and   How to achive this????

3 Answers3

0

If you just want to remove the tags, you could use strip_tags($text);.
echo substr(str_replace("&nbsp;","",strip_tags($yourText),0,50)) would print what you expect.

venkatKA
  • 2,399
  • 1
  • 18
  • 22
0

You should do it in PHP as below example

$data = '<b>Description: </b> Getting Started contains a list of tasks you might want to perform when you set up your computer. Tasks in Getting Started include:'

// remove html tags
$data = strip_tags($data);

// remove all &nbsp;

$data = preg_replace("/\[(.*?)\]\s*(.*?)\s*\[\/(.*?)\]/", "[$1]$2[/$3]", html_entity_decode($data));

OR

$data = trim(str_replace('&nbsp;','',$data));

//limit characters

$data = substr($data,0,50);

now you can echo this data anywhere in your html

GBD
  • 15,847
  • 2
  • 46
  • 50
0
$tmp = "<p><strong><span style=\"font-family: monospace; 
       white-space: pre-wrap;\">Description:Getting Started contains a list 
       of tasks you might want to perform when you set up your computer. Tasks 
       in Getting Started include: &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;
       &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;
       Dnescription: Getting Started contains a list of tasks you might want 
       to perform when you set up your computer. Tasks in Getting Started 
       include:</span></strong></p>";

$tmp = preg_replace("/&#?[a-z0-9]{2,8};/i","",$tmp);

$final = substr(strip_tags($tmp),0,50);

EDIT:

As stated, this doesn't remove html entities. So I borrowed a regexp from How to remove html special chars?

Now, $final contains your desired output.

Community
  • 1
  • 1
BudwiseЯ
  • 1,846
  • 2
  • 16
  • 28