PHP多重划分字符串与PHP

时间:2013-06-27 19:31:37

标签: php string divide

我对此没有什么问题,我完全是初学者,我需要帮助,如何用php划分字符串。我有这个代码并且只有在字符串中有一个分隔符时才能正常工作,但我需要完全控制分割的内容和方法。这是yootheme warp modules.php的一些修改代码。我在字符串' ||'中有3个分隔符。或者' #|'或" |#",可能是字符串或不是字符串。来自joomla模块标题名称的$title$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>';
    }
}

String是简单的纯文本名称,可以用分隔符分隔。例如:

Text 1 |# Text 2 || Text 3 #| Text 4

我的问题是如何一起工作。

&#39; ||&#39; - 将字符串分成两部分,左部分必须在<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>

&#39; #|&#39; - 将字符串分为两部分,左部分放在<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>

&#39; |#&#39; - 将字符串分为两部分,左部分放在<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

当我写作时,我发生了另外一件事,我不能使用两个&#39; #|&#39;或者&#39; #|&#39;没有&#39; ||&#39;的分隔符在字符串中,或​​者不会更好地使用sperator&#39; |#&#39;并且&#39; #|&#39;控制此分隔符在<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编写。谢谢所有想要帮助我的人。非常感谢。

2 个答案:

答案 0 :(得分:0)

如果我理解正确,那么这应该有效:

$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;

答案 1 :(得分:0)

没有测试过,但请试一试:

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函数将匹配任何一个。希望其余的逻辑非常明确。

我发现为变量提供非常清晰的名称可以很好地描述它们包含的内容。让自己更轻松。

相关问题