How do I add text if the htmlspecialchars is blank?? Like for example example.com/name.php?name=Bahirah
if "name=" blank then how do I add text saying something like 'Please enter a name' An example: example.com/name.php?name=
or example.com/name.php
if the url looks like that without anything behind the "=" or no htmlspecialchars at all then I want the text to say 'Please enter a name'
-
[How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve) – buhtz Sep 11 '16 at 06:12
2 Answers
I think you want to check url paramaters for getting currrent url you can look up to below link
code for url is :- $actual_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
now you can check via regex that if name is empty like this
$actual_link="http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$pattern = "/[^=]+$/"; // this will give u full string after last '=' sign
preg_match($pattern,$actual_link , $matches);
print_r($matches); //this will give array
I have tested it and got expected result try it
OK I do not think this is related to htmlspecialchars more to do with strings, substrings and character position. Your question is also quite 'broad' because the string you are looking at would be received after a page or form was submitted, you would then have to analyse the string and inform the user that they needed to enter more data. You would probably be better doing this at the time the form was completed. You could check for any characters after the '=' or '.php' or both or even just check the length of the string returned but I do not know how variable the string returned may be. So based on what you have told me, this may help....
<?php
function AnyThingThere($string, $substring) {
$pos = strpos($string, $substring);
if ($pos === false)
return $string;
else
return(substr($string, $pos+strlen($substring)));
}
echo AnyThingThere('example.com/name.php?name=', '=');
?>
this would return everything after the '='
if I changed the code to
<?php
function AnyThingThere($string, $substring) {
$pos = strpos($string, $substring);
if ($pos === false)
return $string;
else
return(substr($string, $pos+strlen($substring)));
}
echo AnyThingThere('example.com/name.php?name=', '.php');
?>
it would return
'?name'

- 671
- 1
- 5
- 9