I need to remove the beginning part of a URL:
$test = "price/edit.php";
echo ltrim($test,'price/');
shows dit.php
Here is a codepad if you want to fiddle: https://codepad.remoteinterview.io/DominantCalmingBerlinPrice
Any ideas what is going on? I want it to echo edit.php of course.
ltrim removes ALL characters found, consider the following:
$test = 'price/edit.php';
echo ltrim($test, 'dprice/'); // outputs t.php
For this particular scenario, you should probably be using str_replace.
The second argument to ltrim() is a character mask (a list of characters) that should be removed. e is a character that should be removed and so it is removed from edit.
There are many string manipulations that you could use, however since this is a filename/filepath the correct tool is a Filesystem Function, basename():
echo basename($test);
For more information on the filepath check into pathinfo().
Hi I have faced the same problem some time ago and found this solution use it if it suits your need
<?php
$test = "price/edit.php";
echo ltrim(ltrim($test,'price'),'/');
output
edit.php
but i must say you should use basename as all type problem
<?php
$test = "project/price/edit.php";
// echo ltrim(ltrim($test,'price'),'/');// this will give oject/price/edit.phpedit.php
echo basename($test); // and it will generate edit.php
Related
I would like to know why the function addcslashes() is ignoring certain characters.
As you will notice in the output at the bottom, ["`","$","""] are being ignored.
This is my example:
<?php
$ADPasswdRaw = $_GET["element_3"]; #data from a web form
$ADPasswd = addcslashes($ADPasswdRaw, "~`!##$%^&*()_+=-][}{\\|:;\"',./<>?");
echo $ADPasswd;
?>
Output
\~\`\!\#\\\#$\%\^\&\*\(\)\_\+\-\=\;\:"\'\<\>\?\,\.\/
Thanks
This must be a problem with my input.
This is unclear and old at this point.
OK, so I shave my head, but if I had hair I wouldn't need a razor because I'd have torn it all out tonight. It's gone 3am and what looked like a simple solution at 00:30 has become far from it.
Please see the code extract below..
$psusername = substr($list[$count],16);
if ($psusername == $psu_value){
$answer = "YES";
}
else {
$answer = "NO";
}
$psusername holds the value "normann" which is taken from a URL in a text based file (url.db)
$psu_value also holds the value "normann" which is retrieved from a cookie set on the user's computer (or a parameter in the browser address bar - URL).
However, and I'm sure you can guess my problem, the variable $answer contains "NO" from the test above.
All the PHP I know I've picked up from Google searches and you guys here, so I'm no expert, which is perhaps evident.
Maybe this is a schoolboy error, but I cannot figure out what I'm doing wrong. My assumption is that the data types differ. Ultimately, I want to compare the two variables and have a TRUE result when they contain the same information (i.e normann = normann).
So if you very clever fellows can point out why two variables echo what appears to be the same information but are in fact different, it'd be a very useful lesson for me and make my users very happy.
Do they echo the same thing when you do:
echo gettype($psusername) . '\n' . gettype($psu_value);
Since i can't see what data is stored in the array $list (and the index $count), I cannot suggest a full solution to yuor problem.
But i can suggest you to insert this code right before the if statement:
var_dump($psusername);
var_dump($psu_value);
and see why the two variables are not identical.
The var_dump function will output the content stored in the variable and the type (string, integer, array ec..), so you will figure out why the if statement is returning false
Since it looks like you have non-printable characters in your string, you can strip them out before the comparison. This will remove whatever is not printable in your character set:
$psusername = preg_replace("/[[:^print:]]/", "", $psusername);
0D 0A is a new line. The first is the carriage return (CR) character and the second is the new line (NL) character. They are also known as \r and \n.
You can just trim it off using trim().
$psusername = trim($psusername);
Or if it only occurs at the end of the string then rtrim() would do the job:
$psusername = rtrim($psusername);
If you are getting the values from the file using file() then you can pass FILE_IGNORE_NEW_LINES as the second argument, and that will remove the new line:
$contents = file('url.db', FILE_IGNORE_NEW_LINES);
I just want to thank all who responded. I realised after viewing my logfile the outputs in HEX format that it was the carriage return values causing the variables to mismatch and a I mentioned was able to resolve (trim) with the following code..
$psusername = preg_replace("/[^[:alnum:]]/u", '', $psusername);
I also know that the system within which the profiles and usernames are created allow both upper and lower case values to match, so I took the precaution of building that functionality into my code as an added measure of completeness.
And I'm happy to say, the code functions perfectly now.
Once again, thanks for your responses and suggestions.
I've tried to create a code in PHP that gets the topic id after the forward slash in a given string. However the problem i'm having is that its returning nothing, how can i make it return the int?
echo preg_match('/([^/]+)/', 'Learning-English/478', $discussion_id);
echo $discussion_id;
This is for an online forum, thank you for your help; it's much appreciated. If you need ay more information please don't hesitate to leave a comment.
preg_match("#/(\d+)$#", 'Learning-English/478', $discussion_id);
That would work for you.
To print the id(matched number); you need to echo $discussion_id[1].
Here is a working link.
For the newer strings, you wouldn't be needing the string end match($). Thus, the regex will be:
preg_match("#/(\d+)#", 'Looking-for-Pen-Pals/1161&t=viewDiscussion', $discussion_id);
echo $discussion_id[1];
you could just use explode() like this:
$arr = explode('/',$string);
$discussion_id = $arr[count($arr)-1];
this will split your string and then get you the last part.
I'm away from a computer with PHP installed and was wondering what the result of strip_tags() would be on the following text:
"<scr<h1>ipt>alert('oh oh')</scr</h1>ipt>"
Would it return:
"<script>alert('oh oh')</script>" (i.e. not recognize that by removing the obvious tag it exposed a new one)
or
"alert('oh oh')
I know that if it returns the first case I can just repeatedly call the function until I get out what I put in, but I'm curious.
Thanks in advance.
Great question.
Nope, it doesn't strip anything from that string:
<?php
$b = "ipt>alert('oh oh')ipt>";
echo strip_tags($b);
?>
And the output is your original string: ipt>alert('oh oh')ipt>
Edit
In your second case it will print alert('oh oh') so it strips all that is looking like a tag in a single step
It returns just:
alert('oh oh')
I have a txt file on the server which contains 10 lines of text. The text file is rewritten sometimes, and I get new lines using "\r\n". My problem shows when I want to load the lines in javascript variables. I do it like this, but this work only for numbers or for the last line of the file, because its not using the breakline tag: var x = '<?php echo $file[0]; ?>';
Ive tried to alert(x) but it`s not working.... (working only if I read the last line)
Any idead ?
I'm not sure I completely understand the question, but you should be able to use json_encode() which will escape the data appropriately:
var x = <?php echo json_encode($file[0], JSON_HEX_TAG); ?>;
As others have said, you can trim() the data to remove trailing whitespace/line breaks. You should still use json_encode() in addition to this as otherwise it will still be possible to break out of the javascript string (e.g. by sending a string with a quote in).
You want trim():
var x = '<?php echo trim($file[0]); ?>';
But if you read the file(), you could array_map() it with trim() and then keep doing what you're doing:
$values = array_map("trim", file('input-file.txt'));
You might consider what happens if someone decides to include a single quote in the variable with this approach.
EDIT: Then you can add json_encode() as suggested in other answers:
var x = <?php echo json_encode($file[0]); ?>;
Thanks for all! The I solved the problem with the help of artlung and Richard JP Le Guen:
var x = <?php rtrim($file[0]); ?>