This question already has answers here:
Detect HTML tags in a string
(8 answers)
Closed 4 years ago.
I want to do a simple kind of test to see if a string contains any HTML.
In this case if the $string variable is test or <test> it returns no for me:
$string = 'test';
if(strpos($string,'<') !== 'false'){
echo 'no';
}else{
echo 'yes';
}
Is there a better way to check if a string contains HTML? I don't want to do anything to the string just check if it has HTML tags?
if($string != strip_tags($string)) {
// contains HTML
}
Took the answer from here
Related
This question already has answers here:
PHP removing html tags from string
(6 answers)
Closed 7 years ago.
I have a string which is containing some tags, like this;
$str = '<i>this</i> <u class="anything">is</u> a <b>string</b>';
I want this:
$newstr = 'this is a string';
Actually I want to select everything between < and > and then replace it with ''. My target of doing that is defending against CORS and CSRF vulnerability. How can I do that?
Using the strip_tags function will do that for you
<?php
$str = '<i>this</i> <u class="anything">is</u> a <b>string</b>';
echo strip_tags($str);
The result would be
this is a string
Also here is REGEX version:
echo preg_replace('/<[^>]*>/', '', $str); // output: this is a string
This question already has answers here:
How do I check if a string contains a specific word?
(36 answers)
Closed 7 years ago.
I'm writing a PhP script, but for a filter I need to search for a string in a string.
this is the script I have at the moment:
<?php
$stringtocheck = "Schiphol-Almere C./Hilversum/Utrecht C.";
$searchfor = "Hilversum";
// do the magic stuff over here
?>
How do i get the script to search for '$searchfor' in '$stringtocheck'?
Use strpos for to find the needle-string in the haystack-string. And here the haystack-string is stringtocheck and the needle-string is searchfor.
Example of using the function:
<?php
$stringtocheck = "Schiphol-Almere C./Hilversum/Utrecht C.";
$searchfor = "Hilversum";
if(strpos($stringtocheck, $searchfor) !== false)
echo true;
?>
This question already has answers here:
PHP remove all characters before specific string
(4 answers)
Closed 8 years ago.
How can I remove everything before the first ABC in a string?
So that this:
somethingrandomABCotherrandomstuffABCmorerandomstuffABC
turns to this:
otherrandomstuffABCmorerandomstuffABC.
Is this possible with php?
There is a php built in for doing this: strstr
Combine with substr to strip out your token:
$out = substr(strstr($text, 'ABC'), strlen('ABC'))
<?php
function removeEverythingBefore($in, $before) {
$pos = strpos($in, $before);
return $pos !== FALSE
? substr($in, $pos + strlen($before), strlen($in))
: "";
}
echo(removeEverythingBefore("somethingrandomABCotherrandomstuffABCmorerandomstuffABC", "ABC"));
?>
Outputs:
otherrandomstuffABCmorerandomstuffABC
This question already has answers here:
PHP remove all characters before specific string
(4 answers)
Closed 8 years ago.
How can I remove everything before the first ABC in a string?
So that this:
somethingrandomABCotherrandomstuffABCmorerandomstuffABC
turns to this:
otherrandomstuffABCmorerandomstuffABC.
Is this possible with php?
There is a php built in for doing this: strstr
Combine with substr to strip out your token:
$out = substr(strstr($text, 'ABC'), strlen('ABC'))
<?php
function removeEverythingBefore($in, $before) {
$pos = strpos($in, $before);
return $pos !== FALSE
? substr($in, $pos + strlen($before), strlen($in))
: "";
}
echo(removeEverythingBefore("somethingrandomABCotherrandomstuffABCmorerandomstuffABC", "ABC"));
?>
Outputs:
otherrandomstuffABCmorerandomstuffABC
This question already has an answer here:
Odd strpos behavior
(1 answer)
Closed 9 years ago.
I want to clean the url addres but first I want to check if what should be replace is in the string. I want to use strpos because is faster, but it fails to detect the string.
$txt = 'http://www.youtube.com/watch?v=aWFmXFFjJfw';
if(strpos($txt, 'http://www.')){
echo 'true strpos'; // not shows
}
if(strstr($txt, 'http://www.')){
echo 'true strstr'; // show
}
Also
$match = strpos($txt, 'http://www.');
var_dump($match); // int(0);
Because it equals 0 which equates to false.
Instead use:
if(strpos($txt, 'http://www.')!==false){
echo 'true strpos'; //shows
}