Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在“a”的文本中剪切a;链接“;如果超过30个字符_Php_Jquery_String - Fatal编程技术网

Php 在“a”的文本中剪切a;链接“;如果超过30个字符

Php 在“a”的文本中剪切a;链接“;如果超过30个字符,php,jquery,string,Php,Jquery,String,我有一些html链接,比如 <a href="urlThatIneedToCutBecauseItIsTooLongAndWithoutEmptySpaceItDoestGetANewLineWhenItIsPrintedByTheBrowser">urlThatIneedToCutBecauseItIsTooLongAndWithoutEmptySpaceItDoestGetANewLineWhenItIsPrintedByTheBrowser</a> 如果文本长

我有一些html链接,比如

<a href="urlThatIneedToCutBecauseItIsTooLongAndWithoutEmptySpaceItDoestGetANewLineWhenItIsPrintedByTheBrowser">urlThatIneedToCutBecauseItIsTooLongAndWithoutEmptySpaceItDoestGetANewLineWhenItIsPrintedByTheBrowser</a>

如果文本长度超过某些字符(比如30个),我需要剪切文本(而不是链接)。因此,链接必须是这样的:

<a href="urlThatIneedToCutBecauseItIsTooLongAndWithoutEmptySpaceItDoestGetANewLineWhenItIsPrintedByTheBrowser">urlThatIne... ...TheBrowser</a>


我怎么做?正则表达式?我正在使用PHP和jQuery(但我想在服务器端使用)。

可能有一个regex方法,但我会选择一个简单的方法


可能有一个正则表达式方法,但我会选择一个简单的


据我所知,您不是在创建这个页面,而是在解析它。因此,首先,我建议使用dom解析器(我使用:),获取锚元素的内部文本。然后使用substr()方法将其剪切。

据我所知,您不是在创建此页面,而是在解析它。因此,首先,我建议使用dom解析器(我使用:),获取锚元素的内部文本。然后使用substr()方法将其剪切。

如果希望链接文本指示它已被截断

function printLink( $url ){
    $text = strlen( $url ) > 30 
          ? substr( $url, 30 ) . '&hellip;'
          : $url;

    echo '<a href="' . $url . '">' . $text . '</a>';
}

printLink( 'urlThatIneedToCutBecauseItIsTooLongAndWithoutEmptySpaceItDoestGetANewLineWhenItIsPrintedByTheBrowser' );
// <a href="urlThatIneedToCutBecauseItIsTooLongAndWithoutEmptySpaceItDoestGetANewLineWhenItIsPrintedByTheBrowser">urlThatIneedToCutBecauseItIsT…</a>
函数printLink($url){
$text=strlen($url)>30
?substr($url,30)。&hellip;'
:$url;
回声';
}
printLink('UrlThatlinedToctBecauseItToLong and Without EmptySpace在浏览器启动时不获取新的链接');
// 

如果希望链接文本指示它已被截断

function printLink( $url ){
    $text = strlen( $url ) > 30 
          ? substr( $url, 30 ) . '&hellip;'
          : $url;

    echo '<a href="' . $url . '">' . $text . '</a>';
}

printLink( 'urlThatIneedToCutBecauseItIsTooLongAndWithoutEmptySpaceItDoestGetANewLineWhenItIsPrintedByTheBrowser' );
// <a href="urlThatIneedToCutBecauseItIsTooLongAndWithoutEmptySpaceItDoestGetANewLineWhenItIsPrintedByTheBrowser">urlThatIneedToCutBecauseItIsT…</a>
函数printLink($url){
$text=strlen($url)>30
?substr($url,30)。&hellip;'
:$url;
回声';
}
printLink('UrlThatlinedToctBecauseItToLong and Without EmptySpace在浏览器启动时不获取新的链接');
// 
只需使用css即可

<a href="urlThatIneedToCutBecauseItIsTooLongAndWithoutEmptySpaceItDoestGetANewLineWhenItIsPrintedByTheBrowser">urlThatIne... ...TheBrowser</a>

a {  
    white-space: nowrap;  
    overflow: hidden;  
    text-overflow: ellipsis;  
    width: 300px;
    -o-text-overflow: ellipsis;  
    -ms-text-overflow: ellipsis; 
} 

a{
空白:nowrap;
溢出:隐藏;
文本溢出:省略号;
宽度:300px;
-o-text-overflow:省略号;
-ms文本溢出:省略号;
} 
只需使用css即可

<a href="urlThatIneedToCutBecauseItIsTooLongAndWithoutEmptySpaceItDoestGetANewLineWhenItIsPrintedByTheBrowser">urlThatIne... ...TheBrowser</a>

a {  
    white-space: nowrap;  
    overflow: hidden;  
    text-overflow: ellipsis;  
    width: 300px;
    -o-text-overflow: ellipsis;  
    -ms-text-overflow: ellipsis; 
} 

a{
空白:nowrap;
溢出:隐藏;
文本溢出:省略号;
宽度:300px;
-o-text-overflow:省略号;
-ms文本溢出:省略号;
} 

你说得对!在这个简单的例子中,不需要正则表达式。不!这样做还包括
a
href
链接和文本…不仅是文本…@markzzz,您还说要剪切文本。。。我以为你已经解析了DOM。如果您还没有这样做,那么需要一个DOM解析器来获取该标记的内部文本。yasar11732建议在服务器端使用一个.DOM解析器?:O@markzzz对否则,您希望如何解析HTML?这是唯一可靠的方法。这里的例子:你是对的!在这个简单的例子中,不需要正则表达式。不!这样做还包括
a
href
链接和文本…不仅是文本…@markzzz,您还说要剪切文本。。。我以为你已经解析了DOM。如果您还没有这样做,那么需要一个DOM解析器来获取该标记的内部文本。yasar11732建议在服务器端使用一个.DOM解析器?:O@markzzz对否则,您希望如何解析HTML?这是唯一可靠的方法。这里的示例:对于Brad来说,这还包括a、href链接和文本……不仅包括文本……对于Brad来说,这还包括a、href链接和文本……不仅包括文本。。。