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);
Related
$string = "Hello World Again".
echo strrchr($string , ' '); // Gets ' Again'
Now I want to get "Hello World" from the $string [The substring before the last occurrence of a space ' ' ]. How do I get it??
$string = "Hello World Again";
echo substr($string, 0, strrpos( $string, ' ') ); //Hello World
If the character isn't found, nothing is echoed
This is kind of a cheap way to do it, but you could split, pop, and then join to get it done:
$string = 'Hello World Again';
$string = explode(' ', $string);
array_pop($string);
$string = implode(' ', $string);
One (nice and chilled out) way:
$string = "Hello World Again";
$t1=explode(' ',$string);
array_pop($t1);
$t2=implode(' ',$t1);
print_r($t2);
Other (more tricky) ways:
$result = preg_replace('~\s+\S+$~', '', $string);
or
$result = implode(" ", array_slice(str_word_count($string, 1), 0, -1));
$myString = "Hello World Again";
echo substr($myString, 0, strrpos($myString, " "));
strripos — Find the position of the last occurrence of a case-insensitive substring in a string
$string = "hello world again";
echo substr($string, 0, strripos($string, ' ')); // Hello world
You can use a combination of strrpos, which gets the position of the last instance of a given string within a string, and substr to return the value.
The correct implementation should be:
$string = "Hello World Again";
$pos = strrpos( $string, ' ');
if ($pos !== false) {
echo substr($string, 0, $pos ); //Hello World
}
Otherwise if the character is not found it will print nothing. See following case:
$string = "Hello World Again";
//prints nothing as : is not found and strrpos returns false.
echo substr($string, 0, strrpos( $string, ':') );
You could just use:
$string = "Hello World Again";
echo preg_replace('# [^ ]*$', '', $string);
This will work regardless of whether the character occurs in the string or not. It will also work if the last character is a space.
function cutTo($string, $symbol) {
return substr($string, 0, strpos($string, $symbol));
}
<?php
$str = "Hello World!";
echo $str . "<br>";
echo chop($str,"World!");
// output - Hello
?>
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'.";
}
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'
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";
}
i want a php function probably regular expression to find and replace a string that starts with a constant string and ends up with a specific string and the text mentioned within those strings.. e.g.,
[Starting String]..anything.. [Ending String]
and i want to remove above pattern of string with empty space.. Kindly advise!!
$str = preg_replace('/' . preg_quote('[Starting String]') .
'.*?' .
preg_quote('[Ending String]') . '/', '', $str);
preg_quote is used to be sure that you will not break the regexp with some specific for regexp characters in your 'Starting' and 'Ending' strings, like []
For $str = ' blah blah [Starting String] something [Ending String] blah blah'; the result would be blah blah blah blah
I believe you want to use preg_replace. I haven't tested it, but something along the lines of:
$str = "Starting String foo bar Ending String"
$pattern = '/^Starting String(.+)Ending String$/'
$replacement = ''
$result = preg_replace($pattern, $replacement, $str);
You will have to make sure that Starting String and Ending String do not contain any special regex characters or that they are properly escaped.
define('STARTING', 'starting string');
$ending = 'ending string';
$string = 'starting stringSomething Hereending string';
function get_anything($string, $ending, &$anything)
{
if (strpos($string, STARTING) !== 0)
{
return false;
}
if (substr($string, strlen($ending) * -1) != $ending)
{
return false;
}
$anything = substr($string, strlen(STARTING));
$anything = substr_replace($anything, '', strlen($ending) * -1, -1);
return true;
}
if (get_anything($string, $ending, $anything))
{
echo $anything;
}
The function get_anything will return false if the pattern is not found, and true if it is found. The "anything" in the middle will be returned in the third parameter.