【问题标题】:how to remove text between < > example <this is text>如何删除 <> 示例 <this is text> 之间的文本
【发布时间】:2013-09-30 08:34:35
【问题描述】:

我有下面给出的代码。 它让我删除前 6 个字符可以正常工作。现在我想在 jquery 中添加代码以删除

之间的文本

示例:

请帮忙。

<html>
<head>

<script type="text/javascript">
function showName(sel){
   var str='';
   for (var i=0;i<sel.options.length;i++){
       if (sel.options[i].selected){
           str+=(str!='') ? ', '+sel.options[i].value.slice(6) : sel.options[i].value.slice(6);
       }
   }
   sel.form.selectedFruities.value = str;
}
</script>

</head>
<body>
<select style='width: 925px' value='<?php if( $_SERVER['REQUEST_METHOD'] === 'POST' ) { print_r($_POST['users']); } ?>' name='users[]' onchange='showStaffno(this); showEmail(this); showName(this)' multiple>

<?php

$VoCatQrys = "SELECT * FROM manage where status = 1";
$VoCatdbResults = mysql_query($VoCatQrys);
    while($VoCats = mysql_fetch_array($VoCatdbResults ))
    {
?>

        <option class='h4' value='<?php print($VoCats[3]);echo",";print($VoCats[5]);echo" <";print($VoCats[9]);echo">"; ?>'>

    <?php print($VoCats[5]);echo" ( ";print($VoCats[9]);echo" )"; ?>
        </option>

        <?php
        }
        ?>
</select>

<input type="text" id="name" name="selectedFruities" size="131" > 

</form>

</body>
</html>

【问题讨论】:

  • 能否分享生成的html而不是PHP

标签: php jquery string text


【解决方案1】:

试试

function showName(sel) {
    var strs = [], opt;
    for (var i = 0; i < sel.options.length; i++) {
        opt = sel.options[i];
        if (opt.selected) {
            strs.push(opt.value.slice(6).replace(/<.*?>/, ''))
        }
    }
    sel.form.selectedFruities.value = strs.join();
}

【讨论】:

    【解决方案2】:

    尝试使用 PHP 的 substr() 函数

     $string = '<string>';
     $length = strlen($string);
     //subtract 2 from the length since the first char is being cut off and we're not including the final eithet
     $final = substr($string, 1, $length-2);
    

    或者在JS中

    var str = "<string>";
    var length = str.length;
    var final = str.substr(1, parseInt(length-2));
    

    更多信息herehere

    【讨论】:

      【解决方案3】:

      要删除 html 标签,请使用strip_tags

      【讨论】:

      • 问题是去除尖括号之间的文本,而不是去除 HTML 标签。
      • 当一个 php 函数可以解决问题时,为什么要重新发明轮子? strip_tags 删除尖括号之间的文本!
      • strip_tags('&lt;this is text&gt;'); 将返回一个空字符串。
      猜你喜欢
      • 2017-02-28
      • 2019-08-22
      • 1970-01-01
      • 1970-01-01
      • 2013-01-13
      • 2015-07-30
      • 2015-12-26
      • 2010-11-24
      • 1970-01-01
      相关资源
      最近更新 更多