>我对此没有什么问题,我是完全入门的,我需要帮助,如何使用 php 划分字符串。我有这段代码,只有当我在字符串中有一个分隔符时才能正常工作,但我需要完全控制划分的内容和方式。这是来自yootheme warp模块的少量修改代码.php。我在字符串' ||
' 或 ' #|
' 或 "|#
" 中有 3 个分隔符,它们可能在字符串中,也可能不在字符串中。 $title
是从 joomla 模块标题名称$module->title
的。我们的字符串。 $split_color
和$subtitle
我控制模块不同样式的开/关。
$title = $module->title;
$split_color = 1;
$subtitle = 1;
// split title in two colors
if ($split_color) {
$pos = mb_strpos($title, '#|');
if ($pos !== false) {
$title = '<span class="color">'.mb_substr($title, 0, $pos).'</span>'.'<span class="no-color">'.mb_substr($title, $pos + 2).'</span>';
}
}
if ($split_color) {
$pos = mb_strpos($title, '|#');
if ($pos !== false) {
$title = '<span class="no-color">'.mb_substr($title, 0, $pos).'</span>'.'<span class="color">'.mb_substr($title, $pos + 2).'</span>';
}
}
// create subtitle
if ($subtitle) {
$pos = mb_strpos($title, '||');
if ($pos !== false) {
$title = '<span class="title">'.mb_substr($title, 0, $pos).'</span>'.'<span class="subtitle">'.mb_substr($title, $pos + 2).'</span>';
}
}
字符串是简单的纯文本名称,可以用分隔符划分,例如:
Text 1 |# Text 2 || Text 3 #| Text 4
我的问题是如何做到这一点才能一起工作。
'||
' - 将字符串分成两部分,左侧部分必须在<span class="title"></span>
中,右侧部分必须在<span class="title"></span>
中。例如:
字符串 1:Text 1 Text 2 || Text 3 Text 4
结果 1:
<span class="title">Text 1 Text 2</span>
<span class="subtitle">Text3 Text4</span>
' #|
' - 将字符串分成两部分,左侧部分放在<span class="color"></span>
之间,右侧部分放在<span class="no-color"></span>
之间。例如:
字符串 2:Text 1 Text 2 #| Text 3 Text 4
结果 2:
<span class="color">Text 1 Text 2</span>
<span class="no-color">Text3 Text4</span>
' |#
' - 将字符串分成两部分,左部分放在<span class="no-color"></span>
之间,右部分放在<span class="color"></span>
之间。例如:
字符串 2:Text 1 Text 2 |# Text 3 Text 4
结果 2:
<span class="no-color">Text 1 Text 2</span>
<span class="color">Text3 Text4</span>
但可以一次性使用所有分隔符。
字符串 3:Text 1 #|Text 2 || Text 3 |# Text 4
结果 3:
<span class="title">
<span class="color">Text 1</span>
<span class="no-color">Text2</span>
</span>
<span class="subtitle">
<span class="no-color">Text 3</span>
<span class="color">Text4</span>
</span>
所有可能的字符串示例:
字符串 a:Text 1 Text 2 Text 3 Text 4
字符串 b:Text 1 Text 2 || Text 3 Text 4
字符串 c:Text 1 Text 2 #| Text 3 Text 4
字符串 d:Text 1 Text 2 |# Text 3 Text 4
字符串 e:Text 1 #|Text 2 || Text 3 Text 4
字符串 f:Text 1 Text 2 || Text 3 |# Text 4
字符串 g:Text 1 |#Text 2 || Text 3 Text 4
字符串 h:Text 1 Text 2 || Text 3 #| Text 4
字符串 i:Text 1 #| Text 2 || Text 3 #| Text 4
字符串 j:Text 1 |# Text 2 || Text 3 |# Text 4
字符串 k:Text 1 #| Text 2 || Text 3 |# Text 4
字符串 l: Text 1 |# Text 2 || Text 3 #| Text 4
当我写作时,我又想到了一件事,我不能使用两个'#|
'或'#|
'分隔符,而字符串中没有'||
',或者最好不要使用sperator'|#
'和'#|
'来控制这个分隔符在<span class="color"></span>
和外面的东西在<span class="no-color"></span>
之间。 像这样的东西。
字符串:|#Text 1 #| Text |# 2 #| || Text 3 |# Text 4 #|
结果:
<span class="title">
<span class="color">Text 1</span>
<span class="no-color">Text</span>
<span class="color">2</span>
</span>
<span class="subtitle">
<span class="no-color">Text 3</span>
<span class="color">Text4</span>
</span>
我认为它会更好,但一件事是你想怎么做,第二件事是它如何用 php 编写。谢谢所有想要帮助我的人。非常感谢。
理解正确,那么这应该可以工作:
$parts = explode(' || ', $title);
//title ...
$output = '<span class="title">';
$pos = mb_strpos($parts[0], '#|');
if($pos !== false){
$output .= '<span class="color">'.mb_substr($parts[0], 0, $pos).'</span><span class="no-color">'.mb_substr($parts[0], $pos + 2).'</span>';
} else {
$output .= $parts[0];
}
$output .= "</span>n";
if(isset($parts[1])){
//subtitle ...
$output .= '<span class="subtitle">';
$pos = mb_strpos($parts[1], '|#');
if ($pos !== false) {
$output .= '<span class="no-color">'.mb_substr($parts[1], 0, $pos).'</span><span class="color">'.mb_substr($parts[1], $pos + 2).'</span>';
} else {
$output .= $parts[1];
}
$output .= "</span>n";
}
echo $output;
还没有测试过,但试一试:
function get_inner_spans($content) {
#this function will handle "title" content or "subtitle" content
#determine if we need to split this content string - we may not need to
if (mb_strpos($content, '#|')) {
$delimeter = '#|';
} elseif (mb_strpos($content, '|#')) {
$delimiter = '|#';
}
if (isset($delimiter)) {
list($color, $no_color) = explode($delimiter, $content);
$inner_html = '<span class="color">' . $color . '</span>';
$inner_html .= '<span class="no-color">' . $no_color . '</span>';
} else {
#no need to split this string, so just return the original string passed
$inner_html = $content;
}
return $inner_html;
}
#i'm assuming this string will always contain '||'
$input = 'Text 1 Text 2 Text 3 #| Text 4 || Text 5 |# Text 6';
list($title_content, $subtitle_content) = explode('||', $input);
$output_html = '<span class="title">'
. get_inner_spans($title_content)
. '</span>'
. '<span class="subtitle">'
. get_inner_spans($subtitle_content)
. '</span>';
echo $output_html;
我的理解是,字符串的标题和副标题部分都可以包含#|
或|#
。我编写的get_spans
函数将在任何一个上匹配。希望其余的逻辑非常清楚。
我发现为变量提供非常清晰的名称以准确描述它们所包含的内容会有所帮助。让自己更轻松。