douxian1770 2015-01-05 20:18
浏览 34
已采纳

PHP strpos没有找到“<”

I am encountering a rather peculiar behavior of PHP's strpos() function. I need to loop through a string of keywords and output URLs to each keyword..

Lets take the following example string :

$elementText = "Bluffs, Cliffs, Grasses, Oceans, Rocks < Materials";

And here is the function:

<?php
$i = 0;
$hierarchySeparator = " < ";
$subjects = explode(", ", $elementText);

// loop through keywords
foreach ($subjects as $subject) :

    // look for hierarchical keywords
    $found = strpos($subject, $hierarchySeparator);

    if($found !== false) :

        // extract and loop through hierarchical list
        $subSubjects = explode($hierarchySeparator, $subject);
        $j = 1;

        foreach ($subSubjects as $subSubject) : ?>
            <a href="<?php echo url('/mySearch?q=' . $subSubject) ; ?>"><?php echo $subSubject; ?></a>
            <?php
            // Re-ouput all relevant < signs
            if($j < count($subSubjects)) {
                echo " < ";
            }
            $j++;
        endforeach;
    else : ?>
        <a href="<?php echo url('/mySearch?q=' . $subject) ; ?>"><?php echo $subject; ?></a>            
    <?php endif; 

    // output commas to "nicefy" list output
    $i++;
    if($i < count($subjects)) {
        echo ",";
    } 
endforeach; ?>

However, on my server, PHP fails to detect the "<" symbol, therefor does not separate the hierarchical keywords correctly. Even when I try to explode using < as a delimiter, it does not work.

The odd thing is that I can create a test file and run it manually from command line, which executes exactly as desired, but when I try to run it on my server it does not.

Any idea on how to get this resolved?

  • 写回答

1条回答 默认 最新

  • dongyou8701 2015-01-05 20:23
    关注

    Are you sure it's really a < in there, and not something like &lt;? Remember that your browser will essentially "lie" to you, if it's rendering html.

    e.g.

    php > var_dump(strpos('Rocks < Materials', ' < '));
    int(5)
    php > var_dump(strpos('Rocks &lgt; Materials', ' < '));
    bool(false)
    php > var_dump(strpos('Rocks < Materials', ' &lt; '));
    bool(false)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 51单片机中C语言怎么做到下面类似的功能的函数(相关搜索:c语言)
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题