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?