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 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂