How to remove '<' from a string? - php

say, i have a string like $x="History[424]<"; how to remove the last "<" and make the string $x="History[424]"; ... I tried str_replace and don't know, its not working... :(. Thx in advance
for($k=0;$k<$i;$k++) {
$linklabelmod[$k] = str_replace($linklabel[$k], $linklabel[$k]."[$k]", $linklabel[$k]);
//$var= str_replace($linklabel[$k], $linklabelmod[$k], $var);
print $linklabelmod[$k].'< ';
//print $linklabel[$k].' ';
print $link[$k].'<br>';
}

$x = rtrim($x, '<'); // no regex needed

$x = str_replace("<","",$x);
Edit: This replaces all of the "<", but as you mentioned str_replace in your question, this is how it works.

This would ensure that < is only ever removed from the end of the string, and not from anywhere else within the string;
$y = preg_replace('/<$/', '', $x );

Related

Find a string that is not always the same [PHP]

I need help finding something in a variable that isn't always the same, and then put it in another variable.
I know that what I'm looking for has 5 slashes, it starts with steam://joingame/730/ and after the last slash there are 17 numbers.
Edit: It doesn't end with a slash, thats why I need to count 17 numbers after the fifth slash
Assuming what you're looking for looks something like this:
steam://joingame/730/11111111111111/
Then you could use explode() as a simple solution:
$gameId = explode('/', 'steam://joingame/730/11111111111111/');
var_dump($gameId[4]);
or you could use a regex as a more complex solution:
preg_match('|joingame/730/([0-9]+)|', 'steam://joingame/730/11111111111111/', $match);
var_dump($match[1]);
This splits the string into an array then return the last element as the game_id. It doesn't matter how many slashes. It will always return the last one.
$str = 'steam://joingame/730';
$arr = explode("/", $str) ;
$game_id = end($arr);
Following on from what DragonSpirit said
I modified there code so the string can look like
steam://joingame/730/11111111111111
or
steam://joingame/730/11111111111111/
$str = 'steam://joingame/730/11111111111111/';
$rstr = strrev( $str ); // reverses the string so it is now like /1111111111...
if($rstr[0] == "/") // checks if now first (was last ) character is a /
{
$nstr = substr($str, 0, -1); // if so it removes the /
}
else
{
$nstr = $str; // else it dont
}
$arr = explode("/", $nstr) ;
$game_id = end($arr);
Thanks for the help, I've found a solution for the problem. I'm going to post an uncommented version of the code on pastebin, becuase I couldn't get the code saple thing working here.
code

Remove quotes from string inside of array

I have a string that looks like this:
$string = '"excludeIF":["miniTrack","isTriangleHanger","tubeTrack","boxTrack"]';
I need to get rid of the " that are inside of the [] array so it looks like this:
$string = '"excludeIF":[miniTrack, tubeTrack, boxTrack]';
I was trying some regex but I kept getting rid of all of the quotes.
For this particular example:
$string = '"excludeIF":["miniTrack","isTriangleHanger","tubeTrack","boxTrack"]';
preg_match("/((?<=\[).*(?=\]))/", $string, $match);
$change = str_replace('"', "", $match[0]);
$result = preg_replace("/$match[0]/", $change, $string);
What this does is it gets the content inside the square brackets, removes the quotes, then replaces the original content with the cleaned content.
This may run into errors if you have the exact same string outside of square brackets later on, but it should be an easy fix if you understand what I've written.
Hope it helps.
PS. It would also help if you showed us what regexes you were trying, as you were, perhaps, on the right path but just had some misunderstandings.
So yeah I agree with the comment about the XY Problem, but I would still like to try help.
$string = '"excludeIF":["miniTrack","isTriangleHanger","tubeTrack","boxTrack"]';
You will now need to find the start and end positions of the string that you want edited. This can be done by the following:
$stringPosition1 = strpos($string,'[');
$stringPosition2 = strpos($string,']');
Now you have the correct positions you are able to do a substr() to find the exact string you want edited.
$str = substr($string,$stringPosition1,$stringPosition2);
From here you can do a simple str_replace()
$replacedString = str_replace('"','',$str);
$result = '"excludeIF":' . $replacedString;
It is an excellent idea to look at the PHP docs if you struggle to understand any of the above functions. I truly believe that you are only as good at coding as your knowledge of the language is. So please have a read of the following documents:
Str_pos: http://php.net/manual/en/function.strpos.php
Sub_str: http://php.net/manual/en/function.substr.php
Str_replace: http://php.net/manual/en/function.str-replace.php
test this code:
<?php
$string = '"excludeIF":["miniTrack","isTriangleHanger","tubeTrack","boxTrack"]';
$str_array = str_split($string);
$string_new = '';
$id = 0;
foreach ($str_array as $value) {
if($value == '[' || $id != 0){
$id = ($value != ']') ? 1 : 0;
$string_new .= ($value != "\"") ? $value : '' ;
} else {
$string_new .= $value;
}
}
echo $string_new;
//RESULT "excludeIF":[miniTrack,isTriangleHanger,tubeTrack,boxTrack]
?>
Good luck!
EDIT
<?php
$string = '"excludeIF":["miniTrack","isTriangleHanger","tubeTrack","boxTrack"]';
$part = str_replace("\"","",(strstr($string,'[')));
$string = substr($string,0,strpos($string,'[')).$part;
echo $string;
?>
Other possible solution.
Fun with code!

/ (slash) in preg_match

I have this section some/aaa/9321/something from which I want to extract only 9321. "something" always differs, "aaa" is alyways static. So, I used:
$text = "something/aaa/9321/something";
$first = 'aaa/';
$after = '/';
preg_match("/$first(.*)$after/s",$text,$result);
echo $result;
But isn't working. Can somebody please tell me what I need to use?
I've tried this too:
$text = "something/aaa/9321/something";
$first = 'aaa';
preg_match("|$first(.*)|",$text,$result);
echo substr($result['1'], 1, 4);
But between aaa and something not always 4 characters.
Sorry for bad english. Thanks!
You should always preg_quote strings when you want them to be taken literally in a regular expression:
$text = 'something/aaa/9321/something';
$first = preg_quote('aaa/', '/');
$after = preg_quote('/', '/');
preg_match("/$first(.*)$after/s",$text,$result);
echo $result[1]; // '9321'
Demo
The problem was caused by the fact that / is the delimiter in your regex. You could have also solved this problem by using a different delimiter, such as ~, however, you would just run into the same problem as soon as your string had a ~ or any other character with a special meaning like ., or ?. By using preg_quote, you won't run into this problem again.
Have you tried escaping the /?
Instead of
$first = 'aaa/';
$after = '/';
try
$first = 'aaa\/';
$after = '\/';

trying to cut off at the last character that's a space

I need to find out where the last character in the string is where the charachter is a space, and cut it off there. This is the function I'm using, but there seems to be something wrong with the if statement, but I can't figure out what. There is certainly a space in the $text-string.
Let's say I have a string "Hey, my name is Joh". Then it has to be shortened to "Hey, my name is ".
$checkLastChar = false;
$text = $line[2];
while($checkLastChar != true){
for($i = 1; $i <= strlen($text); $i++)
{
if($text[strlen($tekst) - $i] == " ") {
$checkLastChar = true;
$text = substr($text, 1, strlen($text) - $i);
}
}
}
substr($string, 0, strrpos($string, ' '));
Why don't you use rtrim()?
update
Based on the clarification a solution like Nate's seems to be more appropriate.
Have you ever considered using trim()? It strips the spaces from beginning AND end.
Example:
echo trim(' Hello my name is Matt. ');
Try this:
<?php
$str = "dsajhc \tsjdtgsd "; // string with whitespaces
$str = preg_replace( '/(\s+.*)/i', '', $str ); // remove everything after whitespace
?>
Hope it helps.

PHP: best method to trim a substring from a string

Want to process a set of strings, and trim some ending "myEnding" from the end of each string if it exists.
What is the simplest way to do it?
I know that everything is possible with regexp, but thus seems to be a simple task, and I wonder whether a simpler tool for this exists.
Thanks
Gidi
ima go with preg_replace on this one.
$output = preg_replace('/myEnding$/s', '', $input);
Try this:
$s = "foobarmyEnding";
$toRemove = "myEnding";
$len = strlen($toRemove);
if (strcmp(substr($s, -$len, $len), $toRemove) === 0)
{
$s = substr($s, 0, -$len);
}
ideone
rtrim http://php.net/manual/en/function.rtrim.php
$str = "foobarmyEnding";
$str = rtrim($str, 'myEnding');
// str == "foobar"
ltrim is the same deal for the start of a string http://php.net/manual/en/function.ltrim.php
You could also use str_replace to replace a search term with with an empty string but its slower then rtrim/ltrim if you need to amend the start or end of a string

Categories