How to get all the characters between [] using Pregmatch php - php

I have a string
$str = "[xyz.hlp] into asasa jkljk [xyp.htq] zff [xrt.thg]";
I want to get the character from the string and make an array of all those characters . for example for the above string provided I shuould get and array like this
$array("xyz.hlp","xyp.htq","xrt.thg");
I tried using preg_match(); something like this but it didn't work
preg_match('/\[(.*)\]/', $str , $Fdesc);
Thanks in Advance

I got the desired output but by using loop and some php string functions
<?php
$str = "[xyz.hlp] into asasa jkljk [xyp.htq] zff [xrt.thg]";
$i = 0;
while ($i != strrpos($str, "]")) {
$f_pos = strpos($str, "[", $i); // for first position
$l_pos = strpos($str, "]", $f_pos + 1); // for the last position
$value = substr($str, $f_pos, ($l_pos - $f_pos) + 1);
echo $value;
$i = $l_pos;
}
?>

Related

Php get specific word in string

I have a url. I want to parse url. I don't want to get last two value. How can I do?
$str="first-second-11.1268955-15.542383564";
As I wanted
$str="first-second";
I used this code. But I don't want to get - from last value
$arr = explode("-", $str);
for ($a = 0; $a < count($arr) - 2; $a++) {
$reqPage .= $arr[$a] . "-";
}
You can use regular expressions too.Those are patterns used to match character combinations in strings.:
W*((?i)first-second(?-i))\W*
Use the 3rd param of explode() called limit:
$str="first-second-11.1268955-15.542383564";
$arr = explode("-", $str, -2);
$reqPage = implode($arr, "-"); // contains "first-second"
Regex is the fastest way for the string manipulations. Try this.
$str="first-second-11.1268955-15.542383564";
preg_match('/^[a-z]*-{1}[a-z]*/i', $str, $matches);
$match = count($matches) > 0 ? $matches[0] : '';
echo $match;

Remove string after last hyphen [duplicate]

This question already has answers here:
How to remove part of a string after last comma in PHP
(3 answers)
Closed 3 years ago.
I would like to remove the last hyphen and anything after it in a string. After looking I found something that does the first hyphen but not last:
$str = 'sdfsdf-sdfsdf-abcde';
$str = array_shift(explode('-', $str));
Current String
$str = 'sdfsdf-sdfsdf-abcde';
Desired Result
$str = 'sdfsdf-sdfsdf';
You can use this preg_replace:
$repl = preg_replace('/-[^-]*$/', '', $str);
//=> sdfsdf-sdfsdf
-[^-]*$ will match - followed by 0 or more non-hyphen characters before end of line.
You can use strrpos to get the last index, and then use substr to get the desired result.
$str = 'sdfsdf-sdfsdf-abcde';
$pos = strrpos($str , "-");
if ($pos !== false) {
echo substr($str, 0, $pos);
}
You're close. Just use array_pop() instead of array_shift(). array_pop() removes last element of array. You need, of course, use implode() later to put the strign together again.
$arr = explode('-', $str);
array_pop($arr);
$str = implode('-', $arr);
It's important not to do that in one line since array_pop() works on a reference to the array and it modfies it, and then returns only removed element.
There are a few other possible solutions mentions by other answers.
This is a little bulky but it will work for you:
$str = 'sdfsdf-sdfsdf-abcde';
$pieces = explode("-",$str);
$count = count($pieces);
for ($x = 0; $x <= $count - 2; $x++) {
$desired_result .= $pieces[$x].'-';
}
$desired_result = substr($desired_result, 0, -1);
echo $desired_result;
if you have a lot of them you can use this function:
function removeLast($str){
$pieces = explode("-",$str);
$count = count($pieces);
for ($x = 0; $x <= $count - 2; $x++) {
$desired_result .= $pieces[$x].'-';
}
$desired_result = substr($desired_result, 0, -1);
return $desired_result;
}
you call it by:
$str = 'sdfsdf-sdfsdf-abcde';
$my_result = removeLast($str);

Getting Middle of string in php

I have string like this
Ameerpet,|Jeans Corner: 040-50607090#05:45PM/6
want to get the substring after # and before /.
Tried the following
echo substr($str,strpos($str,'#')+1,strpos($str,'/'))
But i will get the whole string after #
Output 05:45PM/6
You may use preg_match
$match = preg_match('~#\K[^/]*(?=/)~', $str);
DEMO
Try This,
echo substr($str,
strpos($str,'#')+1,
strpos($str,'/') - strpos($str,'#') - 1);
The third parameter is not < position > , its < length >
refer this : http://php.net/manual/en/function.substr.php
Syntax of substr is not substr(str , start ,end)
The correct syntax is string substr ( string $string , int $start [, int $length ] )
This is the reason why entire thing is getting printed to achieve the solution you can use the solution provided by #Avinash Raj . However you are trying to get time which is of length 7 then you can use the syntax in this manner
echo substr($str,strpos($str,'#')+1,7)
Thanks
Try this code:
$data = "Ameerpet,|Jeans Corner: 040-50607090#05:45PM/6";
$first = substr($data, strpos($data, "#") + 1);
$splitter = "/";
$pieces = explode($splitter, $first );
echo $pieces[0];
You may do it with substr also
$yourString="Ameerpet,|Jeans Corner: 040-50607090#05:45PM/6";
$valueAfterSpecialChar = substr($yourString, strpos($yourString, "#") + 1);
echo $desiredOutput= substr($valueAfterSpecialChar, 0, strpos($valueAfterSpecialChar, "/"));

Getting value after second - in php

I have E-20-99 in a string i want to get last value 99 and add 1 means 100 and then wants to generate new string E-20-100.
If your string ALWAYS looks like this, you can easily break it without a regex, simply by using explode()
$string = "E-20-99";
$parts = explode('-', $string);
$last_part = $parts[2] + 1;
$parts[2] = $last_part;
$string = implode('-', $parts);
echo $string;
If the string is always E-20-XX you can use
$n = ((int)substr('E-20-99', strlen('E-20-')))+1;
echo 'E-20-' . $n;
If the string might vary a bit more you could use a regualar expressions such as:
$string = 'E-20-99';
preg_match('/(E-\d+-)(\d+)/', $string, $match);
echo $match[1] . ((int)$match[2] + 1);
$old_str = 'E-20-99';
$new_str = preg_replace_callback('/(?<=-)\d+$/', function($matches) {
return $matches[0] + 1;
}, $old_str);

string between, php

as I am new to php, and after googling :) I still could not find what I wanted to do.
I have been able to find start and end position in string that i want to extract but most of the example use strings or characters or integers to get string between but I could not find string bewteen two positions.
For example:
$string = "This is a test trying to extract";
$pos1 = 9; $pos2 = 14;
Then I get lost. I need to get the text between position 9 and 14 of of the string.
Thanks.
$startIndex = min($pos1, $pos2);
$length = abs($pos1 - $pos2);
$between = substr($string, $startIndex, $length);
You can use substr() to extract part of a string. This works by setting the starting point and the length of what you want to extract.
So in your case this would be:
$string = substr($string,9,5); /* 5 comes from 14-9 */
<?php
$string = "This is a test trying to extract";
$pos1 = 9;
$pos2 = 14;
$start = min($pos1, $pos2);
$length = abs($pos1 - $pos2);
echo substr($string, $start - 1, $length); // output 'a test'
?>

Categories