【问题标题】:How to select string from database where start and end with ul如何从以 ul 开头和结尾的数据库中选择字符串
【发布时间】:2019-09-11 07:29:48
【问题描述】:

是否可以从字符串以 UL 开头并以 /UL 结尾的 DB 中选择部分文本?

在数据库产品描述中有一个文本列中有这样的html特殊字符记录

ul><li>Upper line : 2650/300/720<br></li><li>Down line : 2050/600/900 <br></li><li>Height : 480mm<br></li></ul>

但是这个ul周围还有更多的文字和html:

</span></p><div><span style="font-weight: bold;">Size:</span><br></div><p style="margin-bottom: 20px;"></p><ul><li>Upper line : 2650/300/720<br></li><li>Down line : 2050/600/900 <br></li><li>Height : 480mm<br></li></ul>

有没有什么方法可以只选择/提取以ul 开头和结尾的部分?

【问题讨论】:

  • 为什么要对数据进行 HTML 编码?

标签: php mysql substring


【解决方案1】:

是的,您可以使用SUBSTRING_INDEXSUBSTRING

但就个人而言,我更喜欢在 PHP 中提取子字符串。 使用 PHP 的代码示例:

function get_substring($string, $start, $end){
    $string = ' ' . $string;
    $ini = strpos($string, $start);
    if ($ini == 0) return '';
    $ini += strlen($start);
    $len = strpos($string, $end, $ini) - $ini;
    return substr($string, $ini, $len);
}
$fullstring = 'Words words words <ul> you are awesome </ul>';
$myString = get_substring($fullstring, '<ul>', '</ul>');

echo $myString;

我建议您查看此答案以获取更多代码示例:How to get a substring between two strings in PHP?

【讨论】:

  • 你能给我任何用php做的例子吗?
  • 我已经编辑了原始答案并包含了一段 PHP 代码
猜你喜欢
  • 2021-04-10
  • 2016-10-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-18
  • 1970-01-01
相关资源
最近更新 更多