Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/471.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 将标题一分为二,或尽可能靠近中间位置_Php_Javascript - Fatal编程技术网

Php 将标题一分为二,或尽可能靠近中间位置

Php 将标题一分为二,或尽可能靠近中间位置,php,javascript,Php,Javascript,所以我有这个H3的标题,我想把它的一半,并把它包装在一个跨度。 问题是标题可以有4个单词,或者2个或5个,依此类推,我只知道如何将标题多多少少分成两半 所以我想从: <h3>Some random title goes here</h3> 为此: <h3>Some random <span class="green">title goes here</span></h3> PHP或JavaScript,什么都行。如果您有

所以我有这个H3的标题,我想把它的一半,并把它包装在一个跨度。 问题是标题可以有4个单词,或者2个或5个,依此类推,我只知道如何将标题多多少少分成两半

所以我想从:

<h3>Some random title goes here</h3>
为此:

<h3>Some random <span class="green">title goes here</span></h3>

PHP或JavaScript,什么都行。

如果您有标题,请获取其长度。然后,从长度的一半开始搜索下一个单词边界-通常这是一个空格字符。一旦你有了那个位置,你就知道在哪里分手了

$iSplit = strpos($title, ' ', strlen($title) / 2);
$sFirst = substr($title, 0, $iSplit);
$sSecond = substr($title, $iSplit + 1);

或者可以看一看。

如果你有标题,就看它的长度。然后,从长度的一半开始搜索下一个单词边界-通常这是一个空格字符。一旦你有了那个位置,你就知道在哪里分手了

$iSplit = strpos($title, ' ', strlen($title) / 2);
$sFirst = substr($title, 0, $iSplit);
$sSecond = substr($title, $iSplit + 1);

或者看看。

把它分成几个字,然后大致在中间插入即可:

<?PHP
$string = 'this is an odd string';
$insert = '<tag>';
$words = explode( ' ', $string );
$half = round( count( $words ) / 2 );
array_splice( $words, $half, 0, $insert );
print join( ' ', $words );
?>

将其拆分成几个字,然后大致在中间插入即可:

<?PHP
$string = 'this is an odd string';
$insert = '<tag>';
$words = explode( ' ', $string );
$half = round( count( $words ) / 2 );
array_splice( $words, $half, 0, $insert );
print join( ' ', $words );
?>

我不确定你想让这件事有多复杂。如果你不想让它在文字上分裂,你对在标题的中间点简单地注入跨度感到满意吗?当然,这意味着在单词的开头没有考虑注入

在字符级注入:

<?php

$title = 'Some random title goes here';
$half_index = floor(strlen($title)/2);
$split_title = substr($title, 0, $half_index) . '<span class="green">' . substr($title, $half_index) . '</span>';

?>

<h3><?php echo $split_title ?></h3>
在单词级注入:

<?php

$title = 'Some random title goes here';
$words = preg_split('/ /', $title);
$half_index = floor(count($words)/2);
$split_title =
    implode(' ', array_slice($words, 0, $half_index)) . ' '.
    '<span class="green">' . implode(' ', array_slice($words, $half_index)) . '</span>';

?>

<h3><?php echo $split_title ?></h3>

我不确定你想让这件事有多复杂。如果你不想让它在文字上分裂,你对在标题的中间点简单地注入跨度感到满意吗?当然,这意味着在单词的开头没有考虑注入

在字符级注入:

<?php

$title = 'Some random title goes here';
$half_index = floor(strlen($title)/2);
$split_title = substr($title, 0, $half_index) . '<span class="green">' . substr($title, $half_index) . '</span>';

?>

<h3><?php echo $split_title ?></h3>
在单词级注入:

<?php

$title = 'Some random title goes here';
$words = preg_split('/ /', $title);
$half_index = floor(count($words)/2);
$split_title =
    implode(' ', array_slice($words, 0, $half_index)) . ' '.
    '<span class="green">' . implode(' ', array_slice($words, $half_index)) . '</span>';

?>

<h3><?php echo $split_title ?></h3>

好吧,这会管用的。但它相当丑陋

var old = $('h3').text();
var words = old.split(' ');
var new_text = words[0] + ' ' + words[1] + ' ' + words[2];
var span_content =  ''; 
for(i = 3; i < words.length; i++){
    span_content += words[i] + ' '
}
span_content = span_content.trim();
new_text += '<span class="green">' + span_content + '</span>';
$('h3').html(new_text);
如果您只想隐藏文本,也可以在CSS中执行类似的操作。它被称为文本溢出:省略号

好吧,这会管用的。但它相当丑陋

var old = $('h3').text();
var words = old.split(' ');
var new_text = words[0] + ' ' + words[1] + ' ' + words[2];
var span_content =  ''; 
for(i = 3; i < words.length; i++){
    span_content += words[i] + ' '
}
span_content = span_content.trim();
new_text += '<span class="green">' + span_content + '</span>';
$('h3').html(new_text);
如果您只想隐藏文本,也可以在CSS中执行类似的操作。它被称为文本溢出:省略号
在javascript中,您可以使用jQuery:

$('h3').each(function(i,el){
  el = $(el);
  var title = el.text().split(' ');
  el.html(
    title.splice(0, Math.floor(title.length/2)).join(' ')
      + ' <span>' + title.join(' ') + '</span>'
  );
});
这应该是可行的,但还没有测试过

编辑: 在评论中,问题出现了,如果我们只有一个词,那么最后还有一个词。为了防止这种情况,我们可以事先检查:

$('h3').each(function(i,el){
  el = $(el);
  var title = el.text().split(' ');
  if(title.length === 1)
    return;
  el.html(
    title.splice(0, Math.floor(title.length/2)).join(' ')
      + ' <span>' + title.join(' ') + '</span>'
  );
});

在javascript中,可以使用jQuery:

$('h3').each(function(i,el){
  el = $(el);
  var title = el.text().split(' ');
  el.html(
    title.splice(0, Math.floor(title.length/2)).join(' ')
      + ' <span>' + title.join(' ') + '</span>'
  );
});
这应该是可行的,但还没有测试过

编辑: 在评论中,问题出现了,如果我们只有一个词,那么最后还有一个词。为了防止这种情况,我们可以事先检查:

$('h3').each(function(i,el){
  el = $(el);
  var title = el.text().split(' ');
  if(title.length === 1)
    return;
  el.html(
    title.splice(0, Math.floor(title.length/2)).join(' ')
      + ' <span>' + title.join(' ') + '</span>'
  );
});
这个怎么样:

使用允许你不在单词的中间分裂。然后,根据wordwrap返回的内容,您可以使用仅将字符串拆分为两个块,最后用于格式化。

这样如何:


使用允许你不在单词的中间分裂。然后,根据wordwrap返回的内容,使用仅将字符串拆分为两个块,最后用于格式化。

除非您希望按像素大小拆分为两半。众所周知的英语谚语“WWIIIIII”可能是这种方法严重失败的最好例子。不过,对于大多数其他文本,它都可以正常工作@GolezTrol为什么会失败?我当然会使用单空格字体:是的,我也这样做了,把文本标记为代码。不得不修改我的评论来证明我的观点-除非你想按像素大小一分为二。众所周知的英语谚语“WWIIIIII”可能是这种方法严重失败的最好例子。不过,对于大多数其他文本,它都可以正常工作@GolezTrol为什么会失败?我当然会使用单空格字体:是的,我也这样做了,把文本标记为代码。不得不修改我的评论来证明我的观点-也许其他的例子也有用,但这离我很近。谢谢你,伙计!很好!也许其他的例子也有用,但这离我很近。谢谢你,伙计!很好!我会测试这个,听起来也很棒。。无论如何,php和floor似乎可以工作,这也应该可以!谢谢!由于某种原因,这不会开火。。。但总的想法是好的,哈哈。。耶!伟大的人!这比php代码要好得多。。超级骗子!谢谢:问题:如果标题只有一个单词,我们怎么能跳过添加呢?我会测试一下,听起来也不错。。无论如何,php和floor似乎可以工作,这也应该可以!谢谢!由于某种原因,这不会开火。。。但总的想法是好的,哈哈。。耶!伟大的人!这比php代码要好得多。。超级骗子!谢谢:问题:如果标题只有一个单词,我们怎么能跳过添加?