How to search string in string php? - php

I'm searching one PHP function or other solution for my problem.
So, I have a string, this:
$string = 'dfdfdjdkfjsdklfjdksfjekrjekjfdlkfjsdlfdflokuki48(malac)kjdkfdjfkjkejrkerjer';
I'm searching one function, that it has the next parameters:
functionName ($string, $fromCharacters, $toCharacters);
And I run this function:
functionName ($string, 'lokuki48(', ')');
And I will get this: 'malac', or max. this: 'lokuki48(malac)'.
Do you know about PHP function or solution for my problem?
I hope you understand my problem.
Thank you!

try this,
$string = 'dfdfdjdkfjsdklfjdksfjekrjekjfdlkfjsdlfdflokuki48(malac)kjdkfdjfkjkejrkerjer';
if (strpos($string, 'malac') !== false)
{
echo 'true';
}
else
{
echo 'false';
}
https://3v4l.org/SiPvO
i hope it will be helpful.

The strstr() function searches for the first occurrence of a string inside another string.
Example
Find the first occurrence of "world" inside "Hello world!" and return the rest of the string:
<?php
echo strstr("Hello world!","world");
?>

Thanks Guys!
I have found the solution:
`$string = 'dfdfdjdkfjsdklfjdksfjekrjekjfdlkfjsdlfdflokuki48(fasza)kjdkfdjfkjkejrkerjer';
function get_string_between($string, $start, $end){
$string = " ".$string;
$ini = strpos($string,$start);
if ($ini == 0) return "";
$ini += strlen($start);
$len = strpos($string,$end,$ini) - $ini;
return substr($string,$ini,$len);
}
echo get_string_between($string,"lokuki48(",")");`

If you love lókuki, use preg_match:
<?php
function search($string, $start, $end)
{
preg_match("'".$start."(.*)".$end."'sim", $string, $out);
if(isset($out[1])) {
return $out[1];
}
return false;
}
$string = 'dfdfdjdkfjsdklfjdksfjekrjekjfdlkfjsdlfdflokuki48(malac)jdkfdjfkjkejrkerjer';
$start="lokuki48\(";
$end="\)";
$res = search($string, $start, $end);
echo $res."\n\n";
$string = 'dfdfdjdkfjsdklfjdksfjekrjekjfdlkfjsdlfdftestfunction(param1, param2)jdkfdjfkjkejrkerjer';
$start="testfunction\(";
$end="\)";
$res = search($string, $start, $end);
echo $res;

Related

Get string between numbers using PHP

How can I adapt this function below to get string between numbers?
Function:
function getInbetweenStrings($start, $end, $str){
$matches = array();
$regex = "/$start([a-zA-Z0-9_]*)$end/";
preg_match_all($regex, $str, $matches);
return $matches[1];
}
Example:
1448459casa48912687
010697046a-parede-verde698012
456902sim-senhora4401254
Output expected:
casa
a-parede-verde
sim-senhora
Thank you.
#edit: I don't want to remove all numbers, I need them! I just want grab string between.
You can use this regex, which looks for a minimal sequence (by adding ? to .*) of characters between starting and trailing digits:
^\d+(.*?)\d+$
In PHP:
$strings = array('1448459casa48912687',
'010697046a-parede-verde698012',
'456902sim-senhora4401254',
'12345number10-downing-street54321');
foreach ($strings as $string) {
$between = preg_replace('/^\d+(.*?)\d+$/', '$1', $string);
echo "$between\n";
}
Output:
casa
a-parede-verde
sim-senhora
number10-downing-street
Demo on 3v4l.org
If the strings are different (ie: [foo] & [/foo])
function get_string_between($string, $start, $end){
$string = ' ' . $string;
$ini = strpos($string, $start);
if ($ini == 0) return '';
$ini += strlen($start);
$len = strpos($string, $end, $ini) - $ini;
return substr($string, $ini, $len);
}
$fullstring = '010697046a-parede-verde698012';
$parsed = get_string_between($fullstring, '010697046a', '698012');
echo $parsed; // (result = a-parede-verde)

How to get the last occurrence of a string?

I have a function that extracts the content between a $start and $end inside a $string
function get_string_between($string, $start, $end){
$string = " ".$string;
$ini = strpos($string,$start);
if ($ini == 0) return "";
$ini += strlen($start);
$len = strpos($string,$end,$ini) - $ini;
return substr($string,$ini,$len);
}
The problem with this is that it will only get me the first occurrence found in the string. Per example:
$text = "John has 13 oranges and Jane has 8 oranges";
$how_many_oranges = get_string_between($text,"has "," oranges");
echo $how_many_oranges; // echos "13"
I need it to find the last occurrence inside the string, so that $how_many_oranges = "8";
As mentioned in my comment, swap out strpos() for strrpos()
Find the numeric position of the last occurrence of needle in the haystack string.
Also, I believe you should change
if ($ini == 0)
to
if ($ini === false)
The former will match if $start is at the very beginning of $string. The latter will match if $start cannot be found at all.
Demo ~ https://eval.in/1027283
What about using just PHP's built in function?!
strripos() – Finds the position of the last occurrence of a string inside another string. It is a Case-insensitive.
PHP Docs
I took the liberty to rewrote your code, to make it, IMHO, more readable and self-explanatory :)
I'd also recommend you to use the strrpos:
function getStringBetween($string, $start, $end) {
$lastStartIndex = strrpos($string, $start);
$lastEndIndex = strrpos($string, $end);
$substringStartIndex = $lastStartIndex + strlen($start);
$substringSize = $lastStartIndex - $lastEndIndex - 1;
return substr($string, $substringStartIndex, $substringSize);
}
$text = "John has 13 oranges and Jane has 8 oranges";
$how_many_oranges = getStringBetween($text, "has", "oranges");
echo "'" . $how_many_oranges . "'"; // echos "' 8 '"
Try this:
function get_string_between($string, $start, $end){
$p1 = explode($start,$str);
for($i=1;$i<count($p1);$i++){
$p2 = explode($end,$p1[$i]);
$p[] = $p2[0];
}
return $p;
}
source: stackoverflow.com/a/45033158
This looks like a reasonable time to use regular expressions.
function get_string_between($string, $start, $end) {
$clean_start = preg_quote($start, '/');
$clean_end = preg_quote($end, '/');
$pattern = "/.*$clean_start (.*) $clean_end.*/U"; // U modifier => ungreedy
$default_result = ''; // returned if no matches found
$matches;
preg_match_all($pattern, $string, $matches);
return (count($matches[1]) > 0) ? end($matches[1]) : $default_result;
}
echo get_string_between('John has 13 oranges and Jane has 8 oranges', 'has', 'oranges'); // 8

PHP get string between tag [Multiple tag in one string]

I've been searching a lot but i cannot find anything similar to my problem. I've found this link but i don't think they really answered the question.
Let say i have this string
$my_string = "I am with a [id]123[/id] and [id]456[id]";
I want to get all the number between [id][/id]. I only have this function to get the string between.
function get_string_between($string, $start, $end){
$string = ' ' . $string;
$ini = strpos($string, $start);
if ($ini == 0) return '';
$ini += strlen($start);
$len = strpos($string, $end, $ini) - $ini;
return substr($string, $ini, $len);
}
$fullstring = 'I am with a [id]123[/id] and [id]456[id]';
$parsed = get_string_between($fullstring, '[id]', '[/id]');
But this function only returns the first string found on $fullstring. Maybe i can get 123,456 or in array form array('123','456'). I really am stuck with this.
You can simply use preg_match_all function of the PHP along with following regex
~\[id\](.*?)\[\/id\]~
like as
$my_string = "I am with a [id]123[/id] and [id]456[/id]";
preg_match_all("~\[id\](.*?)\[\/id\]~",$my_string,$m);
print_r($m[1]);

Get string between two strings

My string is: "reply-234-private", i want to get the number after "reply-" and before "-private", it is "234". I have tried with following code but it returns an empty result:
$string = 'reply-234-private';
$display = preg_replace('/reply-(.*?)-private/','',$string);
echo $display;
You can just explode it:
<?php
$string = 'reply-234-private';
$display = explode('-', $string);
var_dump($display);
// prints array(3) { [0]=> string(5) "reply" [1]=> string(3) "234" [2]=> string(7) "private" }
echo $display[1];
// prints 234
Or, use preg_match
<?php
$string = 'reply-234-private';
if (preg_match('/reply-(.*?)-private/', $string, $display) === 1) {
echo $display[1];
}
Have a look at explode() function
something like this:
$myString = 'reply-234-private';
$myStringPartsArray = explode("-", $myString);
$answer = $myStringPartsArray[1];
This article show you, How to get of everything string between two tags or two strings.
http://okeschool.com/articles/312/string/how-to-get-of-everything-string-between-two-tag-or-two-strings
<?php
// Create the Function to get the string
function GetStringBetween ($string, $start, $finish) {
$string = " ".$string;
$position = strpos($string, $start);
if ($position == 0) return "";
$position += strlen($start);
$length = strpos($string, $finish, $position) - $position;
return substr($string, $position, $length);
}
?>
in case your question, you can try this :
$string1='reply-234-private';
echo GetStringBetween ($string1, "-", "-")
or we can use any 'identifier string' for grab the string between the identifier string. for example:
echo GetStringBetween ($string1, "reply-", "-private")
Use php's inbuilt regex support functions
preg_match_all
this would help, suppose you want the array of strings(keys) between ## in following example, where '/' doesn't fall in-between, you can built new example with different start an end variable
function getInbetweenStrings($start, $end, $str){
$matches = array();
$regex = "/$start([a-zA-Z0-9_]*)$end/";
preg_match_all($regex, $str, $matches);
return $matches[1];
}
$str = "C://##ad_custom_attr1##/##upn##/##samaccountname##";
$str_arr = getInbetweenStrings('##', '##', $str);
print_r($str_arr);
$myString = 'reply-234-private';
echo str_replace('-','',filter_var($myString,FILTER_SANITIZE_NUMBER_INT));
That should do the job.
If you want to do it in js, try this function -
function getStringBetween(str , fromStr , toStr){
var fromStrIndex = str.indexOf(fromStr) == -1 ? 0 : str.indexOf(fromStr) + fromStr.length;
var toStrIndex = str.slice(fromStrIndex).indexOf(toStr) == -1 ? str.length-1 : str.slice(fromStrIndex).indexOf(toStr) + fromStrIndex;
var strBtween = str.substring(fromStrIndex,toStrIndex);
return strBtween;
}

Getting total sub strings in the string

I am trying to get sub string in between two sub strings in string. By using this code
I am getting first sub string only. Can you please say how can I get all sub strings?
Thanks
Sateesh
Using code:
<?php
function get_string_between($string, $start, $end){
$string = " ".$string;
$ini = strpos($string,$start);
$ini += strlen($start);
$len = strpos($string,$end,$ini) - $ini;
return substr($string,$ini,$len);
}
$fullstring = "[tag]php[/tag] [tag]java[/tag] ";
$count = substr_count($fullstring, '[tag]');
for($i=0; $i<$count;$i++){
$parsed = get_string_between($fullstring, "[tag]", "[/tag]");
echo "LineItems[$i]:".$parsed."<br>";
}
?>
$matches = array();
preg_match('#\[tag\]([^\[]+)\[/tag\]#', "[tag]php[/tag] [tag]java[/tag] ", $matches)
$matches == array("[tag]php[/tag] [tag]java[/tag] ", 'php','java');
array_shift($matches);
$matches == array('php','java');
Something along those lines, try incorporating that into your function
This should do what you're looking for using a regular expression. Do note that it won't work if you have multiple [tag]s nested within eachother.
<?
$fullstring = "[tag]php[/tag] [tag]java[/tag]";
$matches = array();
preg_match_all('#\[tag\](.*?)\[/tag\]#', $fullstring, $matches);
foreach ($matches[1] as $match) {
// Do whatever you need to do with the matches here.
echo "$match<br/>";
}

Categories