I am having multiple paragraphs which were coming in loop dynamically. I just want to add a prefix to those paragraphs after checking that a paragraph's first word contains a keyword I am looking for.
I tried many times yet I am unable to get a perfect answer.
$var="Hello, this paragraph is a series of related sentences developing a central idea, called the topic. Try to think idea.";
if (stripos($var, 'hello,') !== false) {
echo 'true';
}
Please help with this. I received many similar, related answers but I didn't get the expected one. I am looking to make it work with with case-insensitive.
I am not 100% sure if understand your question right, but maybe you can try this.
$var = "Hello, this paragraph is a series of related sentences developing a central idea, called the topic. Try to think idea.";
if (stripos($var, 'hello,') === 0) {
// this checks if the searched word is exactly at the beginning of the string
$var = 'YOUR PREFIX -- ' . $var;
}
echo $var;
If you want to replace the first word, then you should use that code.
$var = "Hello, this paragraph is a series of related sentences developing a central idea, called the topic. Try to think idea.";
if (stripos($var, 'hello,') === 0) {
// this checks if the searched word is exactly at the beginning of the string
$var = preg_replace('/hello\,/i', 'REPLACE WITH', $var, 1);
}
echo $var;
You can use strpos
if(strpos($var, 'Hello,') === 0 || strpos($var, 'hello,') === 0){
//Your Logic
}
Related
So I have this code: if I type something with a specific word it will show up, for example, if ($_POST['text'] it will look for the word smile and convert it to some other text $out_smile. This method works well but when it comes to adding text in between the text like "I love to smile" it won’t recognize "smile" it will recognize it as "I love to smile". I intuitively know the reason for that. Is there any way to add a string?
if ($_POST['text'] == "Smile") {
$out_smile = 'My Code here <img src="URL">';
}
I want to do something like this. Is it possible to do something like this?
if (Found in the entire $text if there is a word == "smile") {
$out_smile = 'My Code here <img src="URL">';
}
OR
$Auto_detect_left = "Extra text in the left hand"; //I Dont know how i am gonna do it
$Auto_detect_right = "Extra text in the right hand"; //I Dont know how i am gonna do it
$Out_result = ".$Auto_detect_left.$text.$Auto_detect_right;
if ($_POST['text'] == "$Out_result") {
$out_smile = 'My Code here <img src="URL">';
}
Assuming that you are asking to verify that a string is contained inside different string, what you want is probably strpos.
$haystack = 'arglebarglearglebargle smile!';
$needle = 'smile';
$pos = strpos($haystack, $needle);
if ($pos === false) {
//$needle is not present in $haystack
} else {
//$needle is in $haystack at position $pos
}
Please note the use of ===, its use is mandatory in this case, or it will not always work properly. (Later on you should probably look up the difference between == and === in php.)
I know this question has been asked many times on SO, but I can't seem to get this to work.
I'm trying to use regular expressions to find matches for any Facebook URL, but not when the URL contains "plugins/like" (as in "http://www.facebook.com/plugins/like")
I've come out with the following, and I'm not quite sure why it is not working:
https?://(www)?\.facebook\.[a-zA-Z]{2,4}/((?!plugins/like).)
Am I making a very obvious mistake? Sorry if it doesn't make sense at all, but I've only trying a hand at PHP for the past five months.
Thank you very much for your time and your help.
Edit:
I'm getting matches for any FB URL so far, but it isn't excluding anything that contains plugins/like
$urls = array(
'http://www.facebook.com/',
'https://facebook.com/plugins/foo',
'http://facebook.ru/plugins/like',
);
$pattern = '#^https?://(www\.)?facebook\.[a-z]{2,4}/(?!plugins/like)#';
foreach ($urls as $url) {
echo preg_match($pattern, $url) . PHP_EOL;
}
Okay?
here is a solution using strpos()
$url = 'http://www.facebook.com/test/plugins/like?somestuff';
$matches = array();
if(preg_match('#^https?://(?:www)?\.facebook\.[a-zA-Z]{2,4}/(.*)$#', $url, $matches)
&& strpos($matches[1], "plugins/like") === false) {
// ok
} else {
// nope
}
You forgot to esacpe special characters in the pattern. Characters like : and / must be following a backslash.
https?\:\/\/(www)?\.facebook\.[a-zA-Z]{2,4}\/((?!plugins\/like).)
All is ok with your re (and it works; I've checked). The only thing I would change is write
(?!.*plugins/like)
instead of (?!plugins/like).. That is for cases when plugins goes not direct after the facebook.com/.
I would like to know how I could find out in PHP if a variable only contains 1 word. It should be able to recognise: "foo" "1326" ";394aa", etc.
It would be something like this:
$txt = "oneword";
if($txt == 1 word){ do.this; }else{ do.that; }
Thanks.
I'm assuming a word is defined as any string delimited by one space symbol
$txt = "multiple words";
if(strpos(trim($txt), ' ') !== false)
{
// multiple words
}
else
{
// one word
}
What defines one word? Are spaces allowed (perhaps for names)? Are hyphens allowed? Punctuation? Your question is not very clearly defined.
Going under the assumption that you just want to determine whether or not your value contains spaces, try using regular expressions:
http://php.net/manual/en/function.preg-match.php
<?php
$txt = "oneword";
if (preg_match("/ /", $txt)) {
echo "Multiple words.";
} else {
echo "One word.";
}
?>
Edit
The benefit to using regular expressions is that if you can become proficient in using them, they will solve a lot of your problems and make changing requirements in the future a lot easier. I would strongly recommend using regular expressions over a simple check for the position of a space, both for the complexity of the problem today (as again, perhaps spaces aren't the only way to delimit words in your requirements), as well as for the flexibility of changing requirements in the future.
Utilize the strpos function included within PHP.
Returns the position as an integer. If needle is not found, strpos()
will return boolean FALSE.
Besides strpos, an alternative would be explode and count:
$txt = trim("oneword secondword");
$words = explode( " ", $txt); // $words[0] = "oneword", $words[1] = "secondword"
if (count($words) == 1)
do this for one word
else
do that for more than one word assuming at least one word is inputted
Really quick noob question with php:
I'm trying to create a conditional statement where I can check if a variable partially matches a substring.
More specific in my case:
$cal is the name of a file, I want it so if $cal contains ".cfg" it echos some text.
pseudocode:
<?php if ($cal == "*.cfg")
{
echo "Hello ";
}
?>
if (strpos($cal, ".cfg") !== false)
{
echo "some text";
}
EDIT
My proposal for matching exactly "*.cfg":
$cal = "abc.cfgxyz";
$ext = "cfg";
if (strrpos($cal, ".".$ext) === strlen($cal)-strlen($ext)-1)
{
echo "matched";
}
In this case, you can simply look at the last four characters of $cal using substr:
if (substr($cal, -4) === '.cfg') {
echo "Hello ";
}
You should look into regular expressions for more complex problems.
if ( preg_match('/\\.cfg$/',$cal) ) echo "Hello ";
should to it. (Assuming you want to match the end of the input string, otherwise, leave out the $)
for simple patterns strpos will do nicely.
if (strpos ('.cfg', $string) !== false)
{
// etc
}
For more complicated parterns, you want preg_match, which can compare a string against a regular expression.
if (preg_match ('/.*?\.cfg/', $string))
{
// etc
}
The former offers better performance, but the latter is more flexible.
You can use various approaches here. For example:
$cal = explode('.', $cal);
if(last($cal) === "cfg")) {
echo "Hello";
}
Or using regular expressions (which are probably way slower than using strpos for example):
if(preg_match("/\.cfg$", $cal)) {
echo "Hello";
}
I honestly don't know how the explode() version compares to substr() with a negative value (lonesomeday's answer) or strpos() (which has its flaws, Frosty Z's answer). Probably the substr() version is the fastet, while regular expressions are the slowest possible way to do this).
In php is there a way i can check if a string includes a value.
Say i had a string "this & that", could i check if that included "this".
Thanks
UPDATED:
$u = username session
function myLeagues2($u)
{
$q = "SELECT * FROM ".TBL_FIXTURES." WHERE `home_user` = '$u' GROUP BY `compname` ";
return mysql_query($q, $this->connection);
}
That code returns if there is an exact match in the database.
But i put two usernames together like "username1 & username2" and need to check if the username session is present in that field.
Is that a way to do this? It obviously woudn't equal it.
ANOTHER UPDATE:
If i use the like %username% in the sql, will it appear if its just the username. So rather than Luke & Matt being shown, if its just Luke will that show too? I dont want it to, i just want things that arent. Can you put != and LIKE so you only get similar matches but not identical?
Cheers
Use strstr or strpos functions. Although there are regex ways too but not worth for such trivial task there.
Using strstr
if (strstr('This & That', 'This') !== false)
{
// found
}
Using strpos
if (strpos('This & That', 'This') !== false)
{
// found
}
I like stristr the best because it is case insensitive.
Usage example from php.net:
$email = 'USER#EXAMPLE.com';
echo stristr($email, 'e'); // outputs ER#EXAMPLE.com
echo stristr($email, 'e', true); // As of PHP 5.3.0, outputs US
based on your update
I think you want to use the like statement
SELECT * FROM table
WHERE home_user LIKE "%username1%" OR home_user LIKE "%username2%"
GROUP BY compname
The % Is your wild card.
<?php>
$string = 'this & that';
if (strpos($string, 'this') === FALSE) {
echo 'this does not exist!';
} else {
echo 'this exists!';
}
?>
What's noteworthy in the check is using a triple equal (forget the actual name). strpos is going to return 0 since it's in the 0th position. If it was only '== FALSE' it would see the 0 and interpret it as FALSE and would show 'this does not exist'. Aside from that this is about the most efficient way to check if something exists.
preg_match('/this/','this & that');
This returns the number of matches, so if it's 0, there were no matches. It will however stop after 1 match.
Regular expressions can be checked with preg-match.