How to get full substring in a string? - php

I have variable $mystring = "abc+adb" and I am trying to find ab in the $mystring. I want to throw a message which say ab does not exist in $mystring, but the following code keeps picking ab from abc, I want ab to be treated as a standalone substring;
$mystring = 'abc+adb';
$findme = 'bc';
$pos = strpos($mystring, $findme);
if ($pos === false) {
echo "The string '$findme' was not found in the string '$mystring'";
}
else {
echo "The string '$findme' was found in the string '$mystring'";
echo " and exists at position $pos";
}

You shouldn't use strpos for that.
strpos - Find the position of the first occurrence of a substring in a string
You can use a regular expression to do that. By no means am I a master of regular expression, but a simple example that meets your immediate need is:
preg_match('/\bab\b/', $mystring);
The preg_match function will return 1 if successful, 0 if no matches found or false in if there was an error.
$mystring = 'abc+adb';
$findme = 'bc';
if ( preg_match('/\b' . $findme . '\b/',$mystring) ) {
echo "The string '$findme' was not found in the string '$mystring'";
} else {
$pos = strpos($mystring, $findme);
echo "The string '$findme' was found in the string '$mystring'";
echo " and exists at position $pos";
}

Related

Find words in text, php

How to resolve this problem:
Write a PHP program that finds the word in a text.
The suffix is separated from the text by a pipe.
For example: suffix|SOME_TEXT;
input: text|lorem ips llfaa Loremipsumtext.
output: Loremipsumtext
My code is this, but logic maybe is wrong:
$mystring = fgets(STDIN);
$find = explode('|', $mystring);
$pos = strpos($find, $mystring);
if ($pos === false) {
echo "The string '$find' was not found in the string '$mystring'.";
}
else {
echo "The string '$find' was found in the string '$mystring',";
echo " and exists at position $pos.";
}
explode() returns an array, so you need to use $find[0] for the suffix, and $find[1] for the text. So it should be:
$suffix = $find[0];
$text = $find[1];
$pos = strpos($text, $suffix);
if ($pos === false) {
echo "The string '$suffix' was not found in '$text'.";
} else {
echo "The string '$suffix' was found in '$text', ";
echo " and exists at position $pos.";
}
However, this returns the position of the suffix, not the word containing it. It also doesn't check that the suffix is at the end of the word, it will find it anywhere in the word. If you want to match words rather than just strings, a regular expression would be a better method.
$suffix = $find[0];
$regexp = '/\b[a-z]*' . $suffix . '\b/i';
$text = $find[1];
$found = preg_match($regexp, $text, $match);
if ($found) {
echo echo "The suffix '$suffix' was found in '$text', ";
echo " and exists in the word '$match[0]'.";
} else {
echo "The suffix '$suffix' was not found in '$text'.";
}

how i can use inverse of haystack and needle postion with strpos php function

I want to use strpos for find exploded string in the imploded array.
i have a bad list :
for example :
exam
test
i imploded those with space and in final to be like a string exam test and i have a string like this :
my string : example string
i exploded thats and having like this array :
0=> example
1=> string
now i want use strpos in inverse state like that to check example string and return me this : exam
because exam used in example word. therefore i want checking example have exam or test or not.
we know in normal strpos using like this :
$mystring = 'example string';
$mybad = array('exam', 'test');
foreach($mybad as $mybad){
$pos = strpos($mystring, $mybad);
if ($pos === false) {
echo "not found";
} else {
echo "The string '$mybad' was found in the string '$mystring'";
echo " and exists at position $pos";
}
}
but i want doing like the above description i wrote this code but doesn't work true.
$mystring = 'example string';
$mystring = explode(" ", $mystring);
$mybad = array('exam', 'test');
$mybad = implode(" ", $mybad);
foreach($mystring as $mystring){
$pos = strpos($mybad, $mystring);
if ($pos === false) {
echo "not found";
} else {
echo "The string '$mybad' was found in the string '$mystring'";
echo " and exists at position $pos";
}
}
Please help me and tell what method should I use.
thanks all.
I'm not sure if I understood what you wanted to say, but I tried to create some useful out of it. Maybe it's at least a basis to talk about.
$mystring = 'example string';
$mystring_array = explode(" ", $mystring);
$mybad_array = array('exam', 'test');
foreach($mystring_array as $mystring_element) {
foreach($mybad_array as $mybad_element) {
$pos = strpos($mystring_element, $mybad_element);
if($pos === false) {
echo "<br>The string '$mybad_element' was not found in the string '$mystring_element'";
} else {
echo "<br>The string '$mybad_element' was found in the string '$mystring_element'";
echo " and exists at position $pos";
}
}
}
This will output:
The string 'exam' was found in the string 'example' and exists at position 0
The string 'test' was not found in the string 'example'
The string 'exam' was not found in the string 'string'
The string 'test' was not found in the string 'string'

Search a PHP string for whitespace after a particular character

This is my string :
$string = '# somebody and some other stuff';
How could I detect the whitespace after the # character?
If I find the match I would like to do something with original string.
$string2 = '# ';
If I understood correctly you want to find # and remove space?
$string = str_replace("# ", "#", $string);
EDIT
You want this PHP source
<?php
$mystring = 'abc';
$findme = 'a';
$pos = strpos($mystring, $findme);
// Note our use of ===. Simply == would not work as expected
// because the position of 'a' was the 0th (first) character.
if ($pos === false) {
echo "The string '$findme' was not found in the string '$mystring'";
} else {
echo "The string '$findme' was found in the string '$mystring'";
echo " and exists at position $pos";
}
?>
$string = preg_replace('/# (.*)/', '# ', $string);

What is the correct return type of strpos? Searching for the '#' char

For example I have a string I am # the penthouse.
I need to know how to find the character "#" in php string and the position of the character.
I tried strpos but its not working.
Thanks for the help in advance.
EDIT:
I've been using this for get the character:
$text = "I am # the penthouse";
$pos = strrpos($text, '#');
if($pos == true)
{
echo "yes";
}
I would do this
Note, I'm using strpos, not reverse counterpart, strrpos
if (($pos = strpos('I am # the penthouse.', '#') !== false) {
echo "pos found: {$pos}";
}
else {
echo "no # found";
}
Note: Because # could be the first character in a string, strpos could return a 0. Consider the following:
// check twitter name for #
if (strpos('#twitter', '#')) { ... }
// resolves to
if (0) {
// this will never run!
}
So, strpos will explicitly return false when no match is found. This is how to properly check for a substring position:
// check twitter name for #
if (strpos('#twitter', '#') !== false) {
// valid twitter name
}
You can also use the function strpos() for that purpose. Like strrpos() it searches for a substring - or at least a char - in a string but it the returns the first position of that substring or boolean(false) if the substring was not found. So the snippet would look like:
$position = strpos('I am # the penthouse', '#');
if($position === FALSE) {
echo 'The # was not found';
} else {
echo 'The # was found at position ' . $position;
}
Note that there are common pitfalls that come with strpos() and strrpos() in php.
1 . Check Type of the return value!
Imagine the following example :
if(!strpos('#stackoverflow', '#')) {
echo 'the string contains no #';
}
The would output that '#' was not found although the string contains an '#'. Thats because of the weak data typing in PHP. The previous strpos() call will return int(0) because it is the first char in string. But unless you enforce a strict type check using the '===' operator this int(0) will be handle as FALSE. This is the correct way:
if(strpos('#stackoverflow', '#') === FALSE) {
echo 'the string contains no #';
}
2 . Use the correct order of arguments!
The signature of strpos is:
strpos($haystack, $needle [, $start]);
Thats unlike other str* functions in PHP where the $needle is the first arg.
Keep this in mind! ;)
This seems to be working for me in PHP 5.4.7:
$pos = strpos('I am # the penthouse', '#');
What do you mean exactly by strpos is not working?
Look this is working for me, it will also work for you
$string = "hello i am # your home";
echo strpos($string,"#");
i hope this will help -
<?php
$string = "I am # the penthouse";
$desired_char = "#";
// checking whether # present or not
if(strstr($string, $desired_char)){
// the position of the character
$position = strpos('I am # the penthouse', $desired_char);
echo $position;
}
else echo $desired_char." Not found!";
?>

Retrieving Multi Checkboxes from mysql DB

I can't seem to figure this out. I have 4 multi checkboxes and store them using implode... then try to grab them with explode to compare. I need to display form with their values checked so I need to see what they checked and display that box ON by default for admin review. Doesn't seem like Explode is working as it's storing the string on index 0
Storing to DB:
$pulled = implode(",",$pulled);
Retrieving from DB
<?php
$pulled = '{pulled}'; // (expression engine CMS field)
echo "before Explode: $pulled <br>";
// returns: before Explode: Tanker,End/Bottom Dump,Flatbed,Van
$pulled = explode(",",$pulled);
echo "after Explode: <br>";
var_dump($pulled);
// returns: after Explode:
array(1) { [0]=> string(8) "Tanker,End/Bottom Dump,Flatbed,Van" }
$pos = strpos($pulled[0], 'Tanker');
if ($pos === false) {
echo "<br><br>The string 'Tanker' was not found in the string '$pulled[0]'";
} else {
echo "<br>The string 'Tanker' was found in the string '$pulled[0]'";
}
it should be:
if ($pulled[0] != 'Tanker') {
echo "<br><br>The string 'Tanker' was not found in the string '$pulled[0]'";
} else {
echo "<br>The string 'Tanker' was found in the string '$pulled[0]'";
}
because as you see in the result of var_dump, the element 0 of the array is exactly the string you are searching for. So no need to do anything else, just compare it. If this doesn't work, then you are not getting the original string cvorrectly. This code works on my machine:
$pulled = 'Tanker,End/Bottom Dump,Flatbed,Van ';
$pulled = explode(",",$pulled);
if ($pulled[0] != 'Tanker') {
echo "<br><br>The string 'Tanker' was not found in the string '$pulled[0]'";
} else {
echo "<br>The string 'Tanker' was found in the string '$pulled[0]'";
}
Perhaps you meant to look for Tanker in the whole string?
$pos = strpos($pulled, 'Tanker');
if ($pos === false) {
echo "<br><br>The string 'Tanker' was not found in the string '$pulled'";
} else {
echo "<br>The string 'Tanker' was found in the string '$pulled'";
}

Categories