Question: After I run a specific query I obtain a string that matches the name of an investment fund like 'GS Gbl Sov Bd Ptf Base Cap' stored in a list.

What I would like to do is to filter out specific words from the query like 'Cap' and 'Ptf' to obtain the "basic" fund name such as 'GS Gbl Sov Bd Base'.

When I try to use the replace method it gives me the error : 'list' object has no attribute 'replace' or if I try to convert it in a string it just won't recognise certain blacklisted words.

Is there a smarter way to achieve this? Thank you

Answer: 
You need to use any HTML parser to get correct result, but this is approach with regex, you want

```
(<\w+(?:\s+\w+=\"[^"]+\")*)(?=[^>]+(?:<|$))

```

[**demo and some explanation**](https://regex101.com/r/EaPBwA/5)

usage

```
$res = preg_replace('/(<\w+(?:\s+\w+=\"[^"]+\")*)(?=[^>]+(?:<|$))/, '$1>', $str);

```
Using standard PHP extension is always the Best option. But for those who are into the same problem and restricted by the PHP version, this is the function which perfectly put length limit on any HTML string:

```
/**
 * Crops HTML text ensuring valid HTML
 * 
 * @param string    HTML string
 * @param int       The length up to which HTML string is to be limited
 */
protected function limitHtml($html, $length)
{
  // Ignoring style tags for displayable string length
  preg_match_all('/<style>(.*?)<\/style>/s', $html, $cssMatches);
  $html = preg_replace('/<style>(.*?)<\/style>/s', '', $html);
  // css
  $css = '';
  if ( isset($cssMatches[1]) ) {
    foreach ( $cssMatches[1] as $cmatch ) {
      $css .= "<style>$cmatch</style>";
    }
  }      
  $truncatedText = substr($html, 0, $length);
  $pos = strpos($truncatedText, ">");
  if($pos !== false)
  {
      $html = substr($html, 0,$length + $pos + 1);
  }
  else
  {
      $html = substr($html, 0,$length);
  }

  // Relace The Broken Opened Tag From The the end of String
  $lastCorruptopnArrow = strrpos($html, "<");
  $lastCloseArrow = strrpos($html, ">");
  if ( $lastCloseArrow < $lastCorruptopnArrow ) {
    $corruptHTmlString = substr($html, $lastCorruptopnArrow, strlen($html) - $lastCorruptopnArrow);
    $html = preg_replace('/'. preg_quote($corruptHTmlString, '/') . '$/', '', $html);
  }

  preg_match_all('#<(?!meta|img|br|hr|input\b)\b([a-z]+)(?: .*)?(?<![/|/ ])>#iU', $html, $result);

  $openedtags = $result[1];
  preg_match_all('#</([a-z]+)>#iU', $html, $result);
  $closedtags = $result[1];
  $len_opened = count($openedtags);
  if (count($closedtags) == $len_opened)
  {
      return $css . $html;
  }
  $openedtags = array_reverse($openedtags);
  for ($i=0; $i < $len_opened; $i++)
  {
      if (!in_array($openedtags[$i], $closedtags))
      {
          $html .= '</'.$openedtags[$i].'>';
      }
      else
      {
          unset($closedtags[array_search($openedtags[$i], $closedtags)]);
      }
  }
  return $css . $html;
}  

```

Working perfectly in my case. Open to enhance: [limit\_html()](https://github.com/geek92sunny/limit_html/blob/master/limit_html.php)