PHP strpos does not locate "<" - php

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) : ?>
<?php echo $subSubject; ?>
<?php
// Re-ouput all relevant < signs
if($j < count($subSubjects)) {
echo " < ";
}
$j++;
endforeach;
else : ?>
<?php echo $subject; ?>
<?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?

Are you sure it's really a < in there, and not something like <? 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', ' < '));
bool(false)

Related

PHP echo line X times different text

Title might not make complete sense, couldn't think of a way to explain it so I will do a mock PHP example.
<?php
$startNumber = 1;
$endNumber = 15;
$fileExt = '.jpg';
?>
And then on the page it will echo from the $startNumber to the $endNumber in something like a foreach
<?php echo("<img src=\"#.jpg\">"); ?>
Hope this is making sense...
I'd also like to point out that the numbers < 10 start with a 0 so it will need to be in the image source link.
echo is not a function, it's a language construct. Don't use ().
Also you need to use a loop like for() to do that (Also see sidenote of Fred -ii).
$endNumber = 15;
for($i = 1; $i <= $endNumber; $i++) {
if($i < 10) {
$i = "0".$i;
}
echo "<img src=\"".$i.".jpg\" />";
}
If this is not what you asked, add more information.

PHP Explode function. If function

Could someone help suggest in below php explode function, we are displaying script after 5th listing. How is it possible to display script exactly after 5th listing and 10th listing on a page which has more than 10 listings
We tried using
if ($i == 5 & $i== 10)
but it does not work
Below is original code - which displays script after 5th listing
<?php
$listings = explode("<hr/>", $list);
$numberOfListings = count($listings);
for($i = 0; $i < $numberOfListings; ++$i)
{
if ($i == 5)
{ ?>
<script> </script>
<?php }
echo $listings[$i] . "<hr/>";
}
?>
Edit
How is it like - if have to display a separate script on $i==9, could you advise.
Because $i starts at 0 (0 to 9 is 10, whilst 0 to 10 is 11). Try if ($i == 4 || $i== 9), with an or operator.
Also I would not use the && (the and operator), because it is unlikely $i will ever equal both 4 and 9. I'd suggest you read into Truth Tables (and maybe Propositional Calculus) because from seeing what you had tried originally, it would be helpful to understand how a truth table works.
(source: wlc.edu)
You can use the contine, continue is used within looping structures to skip the rest of the current loop iteration and continue execution at the condition evaluation and then the beginning of the next iteration.
$arr = range(0,9);
foreach($arr as $number) {
if($number < 5) {
continue;
}
print $number;
}
Ref: http://php.net/manual/en/control-structures.continue.php
Try using modulus operator
$listings = explode("<hr/>", $list);
$numberOfListings = count($listings);
for($i = 1; $i < $numberOfListings; ++$i)
{
if ($i%5 == 0)
{
echo "in";
?>
<script> </script>
<?php
}
echo $listings[$i-1] . "<hr/>";
}
Here we are looping from 1 and there for $i <= $numberOfListings
and while listing we will use $listings[$i-1]
DEMO CODE AT http://codepad.viper-7.com/lrTOgP

Insert line break after every two rows of database

I have a little script that prints a certain amount of rows in a mysql database.
Is there any way to make it so that after every second row it prints, there is a line break inserted?
Adding a line break after every row is simple, but I don't know how to add one after every other row. Is that possible?
You write "script" but in tags you have PHP, so I suppose you need PHP code:
foreach ($rows as $row) {
if ($i++ % 2) {
// this code will only run for every even row
}
...
}
$i=1;
while ($row = mysql_fetch_array($query))
{
//your code
if ($i % 2 == 0)
echo '<br>';
$i++;
}
add new variable before the loop
$i = 0;
then in your loop add
if ($i != 0 && $i%2 == 0)
echo '<br/>';
Depending on the language, something like this should do it: (in php) (where $arr is an array of results)
$str = '';
$i = 0;
for ($i=0; $i<count( $arr ); $i++)
{
if ( ( $i + 1 ) % 2 === 0 )
{
$str .= $arr[$i] . '<br />';
}
else
{
$str .= $arr[$i];
}
}
echo $str;
Use php and modulo.
such as
if($i % 3)
{
echo '<br />'..
If you need to do this inside the query for some reason, you could use something like
SELECT
<your fields>,
IF (((#rn:=#rn+1) % 3)=0,'<br>','') as brornot
FROM
<your tables and joins>,
(#rn:=0)

How Do I Retain Output Indentation?

I have some PHP code like the following (simplified):
<ul>
<?php
for ($Index = 1; $Index <= 10; $Index++)
{
echo("<li>" . $Index . "</li>\n");
}
?>
</ul>
The problem is that for all lines after the first, the output is without indentation. I want to keep my code neat, so I'd like all the <li> elements to be aligned properly.
I tried outputting tabs before each element with \t, but then the first line is indented more than intended. Outputting the tab after the element means the trailing </ul>'s placement will be messed up.
\r does not work at all.
Are there any tricks to keeping output properly formatted, or do I have to live with messy code?
The first line is more indented than you want it to be because of the extra whitespace at the beginning of this line:
<?php
Since that white space is outside the PHP tags, it is output directly. But because it is outside the PHP tags, it is not included in the loop, and will only affect the first line.
You could do this to help avoid it:
<ul>
<?php
for ($Index = 1; $Index <= 10; $Index++) {
echo "\n <li>$Index</li>";
}
?>
</ul>
...and align the <?php ?> tags at the beginning of the lines, or you could do this:
<ul><?php for ($Index = 1; $Index <= 10; $Index++) { ?>
<li><?php echo $Index; ?></li>
<?php } ?></ul>
...but as ridiculous as it seems, this:
<ul><?php for ($Index = 1; $Index <= 10; $Index++) echo "\n <li>$Index</li>"; ?>
</ul>
...is probably the best way to acheive what you are talking about leaving the least room for ambiguity (different PHP versions seem to handle this slightly differently - for instance in PHP 4.3.10 at least, there is an implicit line break after a ?> tag which does not exist in PHP 5 (I think this may have been a bug). That is one of the reasons I don't use mixed HTML and PHP (although I know many people disagree with me on this point) but what I would rather do is this:
<?php
$out = "<ul>\n";
for ($Index = 1; $Index <= 10; $Index++) $out .= " <li>$Index</li>\n";
$out .= "</ul>";
echo $out;
?>
Use spaces. " " * 8, 16, 20, etc...
Or still use \t in your php, then at the very end echo it all out after replacing \t with spaces ,
echo preg_replace('/^\t/', '4_SPACES_HERE', $output_html);

What is wrong with my syntax here?

The goal is to count the number of paragraphs in a group of users text...
(I will assume its always bigger than 5 paragraphs for this exercise)
Then I want to 1/2 the number of paragraphs, round it down and enter some content(echo "yehhoo") in between.
I do understand the way I have gotten my $newvalue is not very good, would also like help on that...
<?php
$choppedup = explode("<p>",$node->field_long_body[0]['safe']);
$choppedpos = count($choppedup);
$choppedpos2 = $choppedpos/2;
$newvalue = floor($choppedpos2);
//I know this is working to here... the rest not so sure.
for($j = 0; $j < $choppedup; $j ++):
print $choppedup[$j];
if ($j == $newvalue):
echo "yehhoo" ;
endif;
endif;
?>
Thank you
for
...
endfor; # not endif
your $newvalue calculation is not terrible, for the array iteration I'd rather suggest foreach loop (using curly braces):
foreach($choppedup as $ind => $p) {
echo $p;
if ($ind == $newvalue) {
echo 'yehoo';
}
}
"Yehhoo" for curly brackets!
for($j == 0; $j < $choppedup; $j ++) {
print $choppedup[$j];
if ($j == $newvalue) {
echo "yehhoo";
}
}
Why do such a complex loop to count the number of paragraph tags?
You can do something like this:
$sInput = '<p>Hello World</p><p>What is going on</p><p>Blah</p>';
if(preg_match_all('/<p>/', $sInput, $arMatches)!==false)
print count($arMatches[0]) . ' paragraphs<br/>';
Of course the above needs some work to make sure there are text between the paragraph tags, but this should work for you need.

Categories