I'm trying to get strpos working the way I want, but stumbled over a few problems here.
The deal is I want to add a text to the title, if the title includes a specific work.
My title is represented as:
<h4><?php echo $article->name; ?></h4>
Then I want to use a code like this:
$a = '$article->name';
if (strpos($a, 'banana') !== false) {
echo 'is good';
}
However it it fails.
Does anyone know how I can get $a to read my title (which is a gode, not just a text)?
How do I replace the echo is good with a picture? (same issue here as my problem is that I dont understand how to get the code working withi the ''). I know the img src="", but just don't how to get it work in this code.
$a = $article->name;
if (strpos($a, 'banana') !== false) {
echo 'is good';
}
It seems that you are having trouble with variable interpolation in strings. Always good to check what is in the manual.
Basically, if you use single quotes the characters will be taken literally. So, to interpret a variable value inside a string, use double quotes.
Additionally, there is obviously no need to interpolate a single value:
$a = "$article->name"; # not needed
$a = $article->name; # better
And you can also concatenate values instead of interpolating them:
echo "<img src=\"$url\" />";
echo '<img src="' . $url . '" />';
$a = $article->name;
if (strpos($a, 'banana') !== false) {
echo '<img src="'.$article->img.'"/>' ;
}
Im assuming you have the adress of the image on the $article->img
And as others said you have to remove the single quotes in the first line.
Test like that:
<h4>
<?php $a = $article->name; if (strpos($a, 'elstock') !== false) {
$a .= 'is good';
}?>
<?php echo $a; ?>
</h4>
I think get string value from the variable is problems.
$a = '$article->name';
Test like that:
<h4>
<?php
$a = $article->name;
if((strpos($a, "banana")) !== false){
echo $a."is good";
}
?>
</h4>
This was complcated, I had to define the article as $title before I got this working...
<?php $a = strtolower($title." ".$description['title']); if (strpos($a, 'bana') !== false) { echo ' is good'; } ?>
Related
I am trying to check for a word in every created sting.
So I am using "if" after "if" in order to check the word in every sting but also if the word is there to print out every thing in which find it.
But i want if the searched word is not found in ANY of the strings to go to else statement No such word.
But now the code posted "no such word" even if Only ONE of the "IFs" is not ok.
Here is the code:
if(stripos($str, $text)){
/*echo $str ;*/ echo $str = preg_replace("/\b([a-z]*${fwcode}[a-z]*)\b/i","<font color ='red'><b>$1</b></font>",$str);
echo "</br>";
}
if(stripos($str2, $text)){
/*echo $str2 ;*/ echo $str2 = preg_replace("/\b([a-z]*${fwcode}[a-z]*)\b/i","<font color ='red'><b>$1</b></font>",$str2);
}
if(stripos($str3, $text)){
/*echo $str3 ;*/ echo $str3 = preg_replace("/\b([a-z]*${fwcode}[a-z]*)\b/i","<font color ='red'><b>$1</b></font>",$str3);
}
if(stripos($str4, $text)){
/*echo $str4 ;*/ echo $str4 = preg_replace("/\b([a-z]*${fwcode}[a-z]*)\b/i","<font color ='red'><b>$1</b></font>",$str4);
}
else
{
echo "No such word"; *** Now will print it if cannot find the $text in ANY of the $str -ings
}
$str,$str1,$str2... are the strings. $text-> is the variable for search.
Please tell me how to have all the strings checked and displayed if the searched word is there, and if in none of the is found Only Then to have else executed.
Thank you.
I tried to put If statement in IF but didn't work.
Please tell me how to have all the strings checked and displayed if the searched word is there, and if in none of the is found Only Then to have else executed.
Thank you.
Your else statement from your code example only applies to the last if:
$arr_string = [
'string1',
'string2',
'string3',
'string4',
];
$found = false;
foreach ($arr_string as $value) {
if(stripos($value, $text) !== false) {
echo preg_replace("/\b([a-z]*${fwcode}[a-z]*)\b/i","<font color ='red'><b>$1</b></font>",$value);
$found = true;
}
}
if (!$found) {
echo "No such word";
}
This way you can easily check as many strings as you want. Using a loop like foreach or for.
Also read about the use of stripos: https://www.php.net/manual/en/function.stripos.php
This function may return Boolean false, but may also return a
non-Boolean value which evaluates to false.
I am trying to check if a number say 1 is present in a string say 11,12,13 To do this I am using strpos:
<?
$s = "2,3,11,12,13";
$ss = "1";
if(strpos($s, $ss)) echo "success";
else echo "fail";
?>
Here I am expecting the code to give fail as output but it gives success.
Is there any function I can use to exactly match the number in a string. I want the number 1 in the string, not the numbers containing 1 like 11,123,100 etc.
Edit 1: It works fine for #Twinfriends answer but the problem is doing it with multiple numbers.
<?
$s = "2,3,11,12,13";
$ss = "1,2";
if(strpos($s, $ss)) echo "success";
else echo "fail";
?>
This code gives output fail but it should give true.
Edit 2: Check answer https://stackoverflow.com/a/48297002/8490014
The problem has been solved but because of for each loop the output is getting repeated as many times as there are values in the array. How can we get the output repeating the output?
You could do it like this:
<?php
$s = "2,3,11,12,13";
$ss = "1";
$toTest = explode(",", $s);
if(in_array("$ss", $toTest)) {
echo "success";
}
else {
echo "fail";
}
?>
If you've any question considering this code, feel free to ask. (Code tested, works well)
The problem has been solved by adding a foreach loop:
<?
$s = "2,3,11,12,13";
$ss = "1,2,3,5";
$catid = explode(",", $s);
$ocat = explode(",",$ss);
foreach($ocat as $abc) {
if(in_array($abc, $catid)) {
echo "success";
}
else {
echo "fail";
}
}
?>
Try this:
<?php
$s = "2,3,11,12,13";
$ss = "1";
$haystack = explode(",",$s);
$matches = preg_grep ("/$ss/", $haystack);
echo !empty($matches) ? "success" : "fail";
You can check by appending comma after the variable to check occurrence at start, appending comma before the variable to check occurrence at last or appending comma before and after the variable to check occurrence at middle.
if((substr($s, 0, (strlen($ss)+1)) === $ss.',')||(substr($s, (strlen($s)-strlen($ss)-1), strlen($s)) === ','.$ss)||(strpos($s, ','.$ss.','))){
echo "success";
}
else{
echo "fail";
}
I'm trying to detect priority of emails (which are stored as plain text in a variable), each email has a 'comments' section and I'm looking to search for terms like 'urgent' or 'high' within the comments area but nowhere else, as these sort of terms are in other places.
So far, what I've been doing was:
if (stristr($body, 'Comment: urgent')){
$urgent = true;
echo '<b>Urgent.</b>';
}
Obviously this doesn't work for cases where 'urgent' is in a sentence like, 'This is urgent'.
How can I search through $body after the substring "Comment:"?
Thanks!
You can use Regular expression:
<?php
$string = 'rweiuowreuiwuier Comment: higewrwre werwrewre high';
if (preg_match('#Comment: (urgent|high)#i',$string)){
$urgent = true;
echo '<b>Urgent.</b>';
}
However you should consider if someone put inside body Comment: high this mail will be also consider as high
The following function - which could do with a new name, takes three parameters.
$a being the Start Frame
$b being the End Frame
$s being the full string
$a would be 'Comment: '
$b would be whatever is at the end of your comments section
$s would be the email string
return value would be the string inbetween, then run your stripos on the return value.
function getMiddle($a, $b, $s) {
return strstr(substr($s, strpos($s, $a) + strlen($a)), $b, true);
}
Example: #Note, the second parameter would need to be specialized to your email#
if (stripos(getMiddle('Comments: ', 'Sincerely', $email), 'urgent') === false) {
echo "URGENT";
}
You can find a string with the function strpos
$mystring = 'Comment: urgent';
$find_string = 'urgent';
$pos = strpos($mystring, $find_string);
if ($pos !== false) {
echo "I find it!";
} else {
echo "Not found";
}
I am trying to search through a URL for a matching string, but the below code snippet doesn't seem to work.
<?php
$url = "http://www.drudgereport.com";
$search = "a";
$file = file($url);
if (in_array($search,$file)) {
echo "Success!";
} else {
echo "Can't find word.";
}
?>
If you are just searching for an occurrence of a string on the page, you can use
$str = file_get_contents($url);
if (strpos($str, $search) !== false) {
echo 'Success!';
} else {
echo 'Fail';
}
in_array() checks if an array member is equal to your needle.
It is improbable many websites will have a line which is equal to a only.
Also, is allow_url_fopen enabled?
That code will only find a line that has the exact $search string (likely including whitespace). If you're parsing HTML, check PHP's DOMDocument classes. Or, you can use a regex to pull what you need.
As #alex says, check is allow_url_fopen is enabled.
Also you can use strpos to search the string:
<?php
$url = "http://www.drudgereport.com";
$search = "a";
$file_content = file_get_contents($url);
if (strpos($file_content, $search) !== false) {
echo "Success!";
} else {
echo "Can't find word.";
}
?>
Is there a way I can check if a user enters a <p> tag inside a form using PHP?
If you simply want to strip all markup use:
strip_tags ( string $str [, string $allowable_tags ] )
otherwise:
substr_replace ( $string , string $replacement , int $start [, int $length ] )
Depends on why you what to know
1 - PHP.net
If it is posted, you can do something like strstr of '<p>' or one of the similar functions, which will then return the location if it exists or NULL if it doesn't.
<?php if ( strstr ( $body, '<p>' ) == NULL )
echo 'All Clear';
else die ( 'Contains <p>' );
if(empty($_POST['foo'])) {
print "Foo empty";
} else {
if(stristr($_POST['foo'], '<p>')) {
print "Contains P tag";
} else {
print "No P tag";
}
}
You could use javascript or jquery .onFocus event.
Assuming they don't enter anything fancy like <p class="stuff">, you can use a simple strpos() call:
$text = $_POST['name_of_field'];
if (strpos($text, '<p>') !== FALSE) {
die("No <p> tags allowed");
}
If they enter attributes, then you'd most likely need a regex, which has its own basket of problems:
$text = $_POST['name_of_field'];
if (preg_match('/<p.*?>/i', $text)) {
die("No <p> tags allowed");
}
Is this what you mean? Assuming you have the form content in a string variable, something like this should work:
<?php
ini_set('display_errors', 1);
ini_set('error_reporting', E_ALL | E_STRICT);
$string1 = 'Hello <p> world';
$string2 = 'Hello world';
$foundIt1 = strripos($string1, '<p>');
$foundIt2 = strripos($string2, '<p>');
if (false === $foundIt1) {
echo '1. didn\'t find it';
} else {
echo "1. found it at offset $foundIt1";
}
echo "\n";
if (false === $foundIt2) {
echo '2. didn\'t find it';
} else {
echo "2. found it at offset $foundIt2";
}
?>
If you want to replace or remove them:
$new_data = preg_replace("/<p>/", "whatever you want to replace it with here", $_POST['form_field_id_here']);
If you just want to check for them
strpos("<p>", $_POST['form_field_id_here']);
Then read this to make sure you aren't leaving your site open to attackers:
What's the best method for sanitizing user input with PHP?
(Edit: I know, I know. No regex for HTML parsing. IMHO, if all you are doing is checking for tags then a little bit of regex is better than using a huge HTML parser. That said, if you are checking for many tags and things like <p class="something"> then you should look at this: http://docs.php.net/manual/en/domdocument.loadhtml.php )