Find the occurences of a substring in string using php? - php

I need to count the number of occurences of the substring in a string.
my code like,
$str1 = "when he arrives, he want to open the door.";
$find_str = "he";
here the string "he" is occured 4 times.
how can i find it in php?..

Use substr_count:
substr_count($str1, $find_str)

Make use of the substr_count php function.
Solution:
$str1 = "when he arrives, he want to open the door.";
$find_str = "he";
substr_count($str1, $find_str);

echo substr_count($str1, $find_str);

Related

Read number after specific word

I'm trying to read a number in a link in php. But I'm not sure how to do it.
http://example.com/Productdetail.asp?SID=72&ProductID=8640
How can I read the number 8640 without reading 72 at the same time.
ProductID= <- it will always be there.
SID=X <- this one will be there sometimes and left out on other pages. and the number can change between 1-999,
Is there a way to say ProductID= "read the next 4 numbers and save in string"
Thanks
you can get this output by using explode()
$link='http://example.com/Productdetail.asp?SID=72&ProductID=8640';
$links=explode('ProductID=',$link);
echo $link[1]; //this is your number
Simple use
echo $_GET['ProductID'];//8640
You can use preg_match as
$str = "http://example.com/Productdetail.asp?SID=72&ProductID=8640";
preg_match('/&ProductID=(\d+)?/',$str,$match);
echo $match[1];
Or you can simply use parse_str as
$str = "http://example.com/Productdetail.asp?SID=72&ProductID=8640";
parse_str($str,$res);
echo $res['ProductID'];
You can use:
strpos to get the ProductID=
's index
substr to get the string from ProductID= until the end of the string
str_replace to replace the ProductID= part
<?php
$str = "http://example.com/Productdetail.asp?SID=72&ProductID=8640";
echo str_replace("ProductID=", "", substr($str, strpos($str, "ProductID=")));
?>

php - Get string before nth dash (-)

I have a string that looks something like this:
abc-def-ghi-jkl-mno-pqr-stu-vwx-yz I'd like to get the content BEFORE the 4th dash, so effectively, I'd like to get abc-def-ghi-jkl assigned to a new string, then I'd like to get mno assigned to a different string.
How could I go about doing this? I tried using explode but that changed it to an array and I didn't want to do it that way.
Try this:
$n = 4; //nth dash
$str = 'abc-def-ghi-jkl-mno-pqr-stu-vwx-yz';
$pieces = explode('-', $str);
$part1 = implode('-', array_slice($pieces, 0, $n));
$part2 = $pieces[$n];
echo $part1; //abc-def-ghi-jkl
echo $part2; //mno
See demo
http://php.net/manual/en/function.array-slice.php
http://php.net/manual/en/function.explode.php
http://php.net/manual/en/function.implode.php
Can you add your source code? I done this one before but I cant remember the exact source code I used. But I am pretty sure I used explode and you can't avoid using array.
EDIT: Mark M answer is right.
you could try using substr as another possible solution
http://php.net/manual/en/function.substr.php
If I see where you are trying to get with this you could also go onto substr_replace
I guess an alternative to explode would be to find the position of the 4th - in the string and then get a substring from the start of the string up to that character.
You can find the position using a loop with the method explained at find the second occurrence of a char in a string php and then use substr(string,0,pos) to get the substring.
$string = "abc-def-ghi-jkl-mno-pqr-stu-vwx-yz";
$pos = -1;
for($i=0;$i<4;$i++)
$pos = strpos($string, '-', $pos+1);
echo substr($string, 0, $pos);
Code isn't tested but the process is easy to understand. You start at the first character (0), find a - and on the next loop you start at that position +1. The loop repeats it for a set number of times and then you get the substring from the start to that last - you found.

search in fread definition to find string

I have this:
$JunkHead3 = #fread($fp,180);
Echo "$JunkHead3", print this:
al-Arena # DeathMatchde_dust2cstrikeDust2 Only/dwÿÿÿÿDstoqn”;Bhtrl**7 !DEmesyhGBLo6ata7#CN3D0S3GA3M!73 | NestL3 L!oNà‘÷D{V
I need to search for /d and add +1 to print in this case "w"
How to do that ?
I try with array_search:
print(array_search('/d', $JunkHead3)+1);
but without success...
The array_search() function is for arrays, what you have is a string.
You can use strpos() to determine the position and substr() to extract the portion you need.
$index = strpos($JunkHead3, '/d');
$result = substr($JunkHead3, $index+2, 1);
http://www.php.net/manual/en/function.strpos.php
http://www.php.net/manual/en/function.substr.php
However I prefer using regular expressions, even though at first they seem a bit cryptic.
$result = preg_replace('/^.*\\/d(.).*$/', '$1', $JunkHead3);
http://php.net/manual/en/function.preg-replace.php

remove a sentence in php string that contains certain words

I have the following string
"This is very nice. Thanks for your support."
Now I want to remove the line that contains Thanks word
you need to explode the paragraph by fullstop.
lets say your full paragraph is store in $str
$lines_arr = explode(".",$str);
now $lines_arr is an array contains numbers of lines.
now under loop you can check which lines contains "thanks" , if it is then skip it.
$string = '';
for($i=0; $i<(count($lines_arr)-1); $i++){
//with the help of strpos
if(strpos($lines_arr[$i],"Thanks") == false){
$string .= $lines_arr[$i].". ";
}
}
you can use str_replace
echo str_replace("Thanks", "", "This is very nice. Thanks for your support.");
# Output: This is very nice. for your support.
If you expect proper punctuation, you could explode() the string into an array on the full stops and delete the elements with your particular word, then merge all the elements back into a string.

check for number anywhere in a string

$vari = "testing 245";
$numb = 0..9;
$numb_pos = strpos($vari,$numb);
echo substr($vari,0,$numb_pos);
The $numb is numbers from 0 to 9
Where am I wrong here, all I need to echo is testing
You want to cut out the numbers from a string?
$string = preg_replace('/(\d+)/', '', 'String with 1234 numbers');
Use a regular expression to strip numeric characters from your string.
or, use a regular expression to find the first instance of one either way...
Your code won't work as-is, as it'll fail if the number if the first character in the string. (You need to check $numb_pos !== false prior to the substr.)
Irrespective, if you just want to check for the existance of a number in a string, something like the following would probably be more efficient.
$digitMatched = preg_match('/\\d/im', $vari);

Categories