how to Find string length without white spaces and functions in php - php

I have assignment to find the length of string without white space and without using any string function anyone help me please

You can use regular expressions and the function preg_match_all:
$value = "This is a test string.";
$length = preg_match_all ('/[^ ]/' , $value, $matches);
echo $length; //18
Here you can find a working example: https://3v4l.org/IPlJi
explanation:
Between [^ and ] you have to add all characters which should not be count to the length of the string. For example: if you want to filter out the character i and (space) you have to set the following pattern: [^ i].
Code to filter i and (space):
$value = "This is a test string.";
$length = preg_match_all('/[^ i]/' , $value, $matches);
echo $length; //15
be carefull with some characters:
If you want to exclude one of the following characters .^$*+?()[{\| you have to escape them with \. If you want to exclude the . too, you have the following code:
$value = "This is a test string.";
$length = preg_match_all ('/[^ \.]/' , $value, $matches);
echo $length; //18
how to test your pattern:
If you want to test your regular expressions for preg_match_all or other functions like that, you can use the following tool: http://www.phpliveregex.com/

This will work for you:
$string = "this is a nice string with spaces and chars";
$length = 0;
$i = 0;
while(isset($string[$i]))
{
if($string[$i] != ' ') $length++;
$i++;
}
var_dump($length);
var_dump(strlen($string));
Outputs:
int(35)
int(43)

<!doctype html>
<html>
<body>
<center>
<form action="#" method="get">
<br>
<input type="text" name="txt"/>
<br><br>
<input type="submit" name="submit"/>
<br><br>
</form>
<!---------------------------------------------------------------------->
<?php
if(isset($_GET["submit"])) {
$name = $_GET["txt"];
for ($i = 0; isset($name[$i]); $i++) {
$j = $i;
}
for ($k = $j; isset($name[$k]); $k--) {
echo $name[$k];
}
}

Related

Php search replacer

I have a search String: $str (Something like "test"), a wrap string: $wrap (Something like "|") and a text string: $text (Something like "This is a test Text").
$str is 1 Time in $text. What i want now is a function that will wrap $str with the wrap defined in $wrap and output the modified text (even if $str is more than one time in $text).
But it shall not output the whole text but just 1-2 of the words before $str and then 1-2 of the words after $str and "..." (Only if it isn`t the first or last word). Also it should be case insensitive.
Example:
$str = "Text"
$wrap = "<span>|</span>"
$text = "This is a really long Text where the word Text appears about 3 times Text"
Output would be:
"...long <span>Text</span> where...word <span>Text</span> appears...times <span>Text</span>"
My Code (Obviusly doesnt works):
$tempar = preg_split("/$str/i", $text);
if (count($tempar) <= 2) {
$result = "... ".substr($tempar[0], -7).$wrap.substr($tempar[1], 7)." ...";
} else {
$amount = substr_count($text, $str);
for ($i = 0; $i < $amount; $i++) {
$result = $result.".. ".substr($tempar[$i], -7).$wrap.substr($tempar[$i+1], 0, 7)." ..";
}
}
If you have a tipp or a solution dont hesitate to let me know.
I have taken your approach and made it more flexible. If $str or $wrap changes you could have escaping issues within the regex pattern so I have used preg_quote.
Note that I added $placeholder to make it clearer, but you can use $placeholder = "|" if you don't like [placeholder].
function wrapInString($str, $text, $element = 'span') {
$placeholder = "[placeholder]"; // The string that will be replaced by $str
$wrap = "<{$element}>{$placeholder}</{$element}>"; // Dynamic string that can handle more than just span
$strExp = preg_quote($str, '/');
$matches = [];
$matchCount = preg_match_all("/(\w+\s+)?(\w+\s+)?({$strExp})(\s+\w+)?(\s+\w+)?/i", $text, $matches);
$response = '';
for ($i = 0; $i < $matchCount; $i++) {
if (strlen($matches[1][$i])) {
$response .= '...';
}
if (strlen($matches[2][$i])) {
$response .= $matches[2][$i];
}
$response .= str_replace($placeholder, $matches[3][$i], $wrap);
if (strlen($matches[4][$i])) {
$response .= $matches[4][$i];
}
if (strlen($matches[5][$i]) && $i == $matchCount - 1) {
$response .= '...';
}
}
return $response;
}
$text = "text This is a really long Text where the word Text appears about 3 times Text";
string(107) "<span>text</span> This...long <span>text</span> where...<span>text</span> appears...times <span>text</span>"
To make the replacement case insensitive you can use the i regex option.
If I understand your question correct, just a little bit of implode and explode magic needed
$text = "This is a really long Text where the word Text appears about 3 times Text";
$arr = explode("Text", $text);
print_r(implode('<span>Text</span>', $arr));
If you specifically need to render the span tags using HTML, just write it that way
$arr = explode("Text", $text);
print_r(implode('<span>Text</span>', $arr));
Use patern below to get your word and 1-2 words before and after
/((\w+\s+){1,2}|^)text((\s+\w+){1,2}|$)/i
demo
In PHP code it can be:
$str = "Text";
$wrap = "<span>|</span>";
$text = "This is a really long Text where the word Text appears about 3 times Text";
$temp = str_replace('|', $str, $wrap); // <span>Text</span>
// find patern and 1-2 words before and after
// (to make it casesensitive, delete 'i' from patern)
if(preg_match_all('/((\w+\s+){1,2}|^)text((\s+\w+){1,2}|$)/i', $text, $match)) {
$res = array_map(function($x) use($str, $temp) { return '... '.str_replace($str, $temp, $x) . ' ...';}, $match[0]);
echo implode(' ', $res);
}

preg_match_all and preg_replace in php

I have a large xml file (about 2mb) and need to replace all checkboxes and radio with my additional string => <i></i>
example
<input type="checkbox" />
replace with
<input type="checkbox" /><i></i>
the code is:
$file = 'style.xml';
$c = file_get_contents($file);
preg_match_all("#<input.*type=\"(checkbox|radio)\".+? />#i", $c, $m);
if($m)
{
for($i = 0; $i < count($m[0]); $i++)
{
/*$search = trim($m[0][$i]);
$replace = "$search<i></i>";*/
$c = preg_replace("#" . preg_quote($m[0][$i], "#") . "#i", $m[0][$i] . '<i></i>', $c);
}
}
if($fp = #fopen('new-style.xml', 'w'))
{
#flock($fp, 2);
#fputs($fp, $c);
#flock($fp, 3);
#fclose($fp);
}
it works, but sometimes replaced with more than one "I" tag
example
<input type="checkbox" /><i></i><i></i><i></i><i></i>
<input type="radio" /><i></i><i></i><i></i>
my regex is wrong? or something else?
how to make replacement only once for string?
screenshot here
image
There's no need to iterate over matches. You can just do it all with a single preg_replace.
preg_replace("#<input.*type=\"(?:checkbox|radio)\".*? />#i", "$0<i></i>", $c);
It matches any checkbox or radio input tag, and replaces that match with $0<i></i> where the $0 refers to the entire match.

How can I split html value and normal string into different array in php?

Say I have string such as below:
"b<a=2<sup>2</sup>"
Actually its a formula. I need to display this formula on webpage but after b string is hiding because its considered as broken anchor tag. I tried with htmlspecialchars method but it returns complete string as plain text. I am trying with some regex but I can get only text between some tags.
UPDATE:
This seems to work with this formula:
"(c<a) = (b<a) = 2<sup>2</sup>"
And even with this formula:
"b<a=2<sup>2</sup>"
HERE'S THE MAGIC:
<?php
$_string = "b<a=2<sup>2</sup>";
$string = "(c<a) = (b<a) = 2<sup>2</sup>";
$open_sup = strpos($string,"<sup>");
$close_sup = strpos($string,"</sup>");
$chars_array = str_split($string);
foreach($chars_array as $index => $char)
{
if($index != $open_sup && $index != $close_sup)
{
if($char == "<")
{
echo "<";
}
else{
echo $char;
}
}
else{
echo $char;
}
}
OLD SOLUTION (DOESN'T WORK)
Maybe this can help:
I've tried to backslash chars, but it doesn't work as expected.
Then i've tried this one:
<?php
$string = "b&lta=2<sup>2</sup>";
echo $string;
?>
Using &lt html entity it seems to work if i understood your problem...
Let me know
Probably you can give spaces such as :
b < a = 2<sup>2</sup>
It does not disappear the tag and looks much more understanding....
You could try this regex approach, which should skip elements.
$regex = '/<(.*?)\h*.*>.+<\/\1>(*SKIP)(*FAIL)|(<|>)/';
$string = 'b<a=2<sup>2</sup>';
$string = preg_replace_callback($regex, function($match) {
return htmlentities($match[2]);
}, $string);
echo $string;
Output:
b<a=2<sup>2</sup>
PHP Demo: https://eval.in/507605
Regex101: https://regex101.com/r/kD0iM0/1

How to remove part of a string, that's wrapped in <b></b>, with php? [duplicate]

This question already has an answer here:
DOM php delete all tags by tag name
(1 answer)
Closed 7 years ago.
So I have a text being pulled from db:
<b>Object title</b>
<br />
<p>Object description</p>
and I need to remove <b>Object title</b>, I tried using substr, but didn't quite come up with the correct way of putting it together. Can you please show me how I could do that?
Edit
Please note: I need to remove only the first <b></b> element, every other bold part of that string should stay the way it is.
u shoud use preg_match for this problem.
$text = "<b>Object title</b><br /><p>Object description</p>";
if (!preg_match("/<b>.*<\/b>/",$text,$m)) {
str_replace($m[0],"",$text);
}
this should work for u.
if u want remove all u can run a a loop on $m.
This can be obtained without the use of regex as well.
We are using simple PHP string functions here:
Try this:
<?php
function removeTags($subject, $replaceStr){
$strlength = strlen($replaceStr);
$subject = strstr($subject, $replaceStr, true) . substr(strstr($subject, $replaceStr), 0 + $strlength);
return $subject;
}
$str = '<b>Object title</b><b>Object title</b><br /><p>Object description</p><b>Object title</b>';
$brOpenRemoved = removeTags($str, '<b>');
$brCloseRemoved = removeTags($brOpenRemoved, '</b>');
echo $brCloseRemoved; // final output
?>
The function removeTags removes the string provided to it. We have provided tags here.
It only removes first occurrence because we are using functions like strstr() which only work on first occurence of the string.
EDIT
We can use below function to prevent calling the same function again and again
<?php
function removeFirstOccurence($subject, $replaceTag){
$replaceStrings = ['<'.$replaceTag.'>', '</'.$replaceTag.'>'];
foreach($replaceStrings as $replaceStr){
$strlength = strlen($replaceStr);
$subject = strstr($subject, $replaceStr, true) . substr(strstr($subject, $replaceStr), 0 + $strlength);
}
return $subject;
}
$str = '<b>Object title</b><b>Object title</b><br /><p>Object description</p><b>Object title</b>';
$brRemoved = removeFirstOccurence($str, 'b');
echo $brRemoved; // final output
?>
If you are sure that there is your pattern in the text:
//remove first <b> tag
$text = '<b>foo</b>...';
$pos = strpos($text,'<b>');
if ($pos !== false) {
$newtext = substr_replace($text,'',$pos,strlen('<b>'));
}
//remove first </b> tag
$pos = strpos($text,'</b>');
if ($pos !== false) {
$newtext = substr_replace($newtext,'',$pos,strlen('</b>'));
}
You can use $limit to the maximum possible replacements for each pattern in preg_replace
$yourString =
"<b>Object title</b>
<br />
<p>Object description</p>";
$limit = 1;
$pattern = "/<b>(.*)<\/b>/";
$replace = "";
$newString = preg_replace($pattern, $replace, $yourString, $limit);
echo $newString;
output:
<br />
<p>Object description</p>
If you use (.*) you can also delete <b> and </b> tags and just stay with the content:
$limit = 1;
$pattern = "/<b>(.*)<\/b>/";
$replace = "$1";
$newString = preg_replace($pattern, $replace, $yourString, $limit);
echo $newString;
output:
Object title
<br />
<p>Object description</p>
Or keep only tags without content:
$limit = 1;
$pattern = "/<b>(.*)<\/b>/";
$replace = "<b></b>";
$newString = preg_replace($pattern, $replace, $yourString, $limit);
echo $newString;
output:
<b></b>
<br />
<p>Object description</p>

Inverse htmlentities / html_entity_decode

Basically I want to turn a string like this:
<code> <div> blabla </div> </code>
into this:
<code> <div> blabla </div> </code>
How can I do it?
The use case (bc some people were curious):
A page like this with a list of allowed HTML tags and examples. For example, <code> is a allowed tag, and this would be the sample:
<code><?php echo "Hello World!"; ?></code>
I wanted a reverse function because there are many such tags with samples that I store them all into a array which I iterate in one loop, instead of handling each one individually...
My version using regular expressions:
$string = '<code> <div> blabla </div> </code>';
$new_string = preg_replace(
'/(.*?)(<.*?>|$)/se',
'html_entity_decode("$1").htmlentities("$2")',
$string
);
It tries to match every tag and textnode and then apply htmlentities and html_entity_decode respectively.
There isn't an existing function, but have a look at this.
So far I've only tested it on your example, but this function should work on all htmlentities
function html_entity_invert($string) {
$matches = $store = array();
preg_match_all('/(&(#?\w){2,6};)/', $string, $matches, PREG_SET_ORDER);
foreach ($matches as $i => $match) {
$key = '__STORED_ENTITY_' . $i . '__';
$store[$key] = html_entity_decode($match[0]);
$string = str_replace($match[0], $key, $string);
}
return str_replace(array_keys($store), $store, htmlentities($string));
}
Update:
Thanks to #Mike for taking the time to test my function with other strings. I've updated my regex from /(\&(.+)\;)/ to /(\&([^\&\;]+)\;)/ which should take care of the issue he raised.
I've also added {2,6} to limit the length of each match to reduce the possibility of false positives.
Changed regex from /(\&([^\&\;]+){2,6}\;)/ to /(&([^&;]+){2,6};)/ to remove unnecessary excaping.
Whooa, brainwave! Changed the regex from /(&([^&;]+){2,6};)/ to /(&(#?\w){2,6};)/ to reduce probability of false positives even further!
Replacing alone will not be good enough for you. Whether it be regular expressions or simple string replacing, because if you replace the &lt &gt signs then the < and > signs or vice versa you will end up with one encoding/decoding (all &lt and &gt or all < and > signs).
So if you want to do this, you will have to parse out one set (I chose to replace with a place holder) do a replace then put them back in and do another replace.
$str = "<code> <div> blabla </div> </code>";
$search = array("<",">",);
//place holder for < and >
$replace = array("[","]");
//first replace to sub out < and > for [ and ] respectively
$str = str_replace($search, $replace, $str);
//second replace to get rid of original < and >
$search = array("<",">");
$replace = array("<",">",);
$str = str_replace($search, $replace, $str);
//third replace to turn [ and ] into < and >
$search = array("[","]");
$replace = array("<",">");
$str = str_replace($search, $replace, $str);
echo $str;
I think i have a small sollution, why not break html tags into an array and then compare and change if needed?
function invertHTML($str) {
$res = array();
for ($i=0, $j=0; $i < strlen($str); $i++) {
if ($str{$i} == "<") {
if (isset($res[$j]) && strlen($res[$j]) > 0){
$j++;
$res[$j] = '';
} else {
$res[$j] = '';
}
$pos = strpos($str, ">", $i);
$res[$j] .= substr($str, $i, $pos - $i+1);
$i += ($pos - $i);
$j++;
$res[$j] = '';
continue;
}
$res[$j] .= $str{$i};
}
$newString = '';
foreach($res as $html){
$change = html_entity_decode($html);
if($change != $html){
$newString .= $change;
} else {
$newString .= htmlentities($html);
}
}
return $newString;
}
Modified .... with no errors.
So, although other people on here have recommended regular expressions, which may be the absolute right way to go ... I wanted to post this, as it is sufficient for the question you asked.
Assuming that you are always using html'esque code:
$str = '<code> <div> blabla </div> </code>';
xml_parse_into_struct(xml_parser_create(), $str, $nodes);
$xmlArr = array();
foreach($nodes as $node) {
echo htmlentities('<' . $node['tag'] . '>') . html_entity_decode($node['value']) . htmlentities('</' . $node['tag'] . '>');
}
Gives me the following output:
<CODE> <div> blabla </div> </CODE>
Fairly certain that this wouldn't support going backwards again .. as other solutions posted, would, in the sense of:
$orig = '<code> <div> blabla </div> </code>';
$modified = '<CODE> <div> blabla </div> </CODE>';
$modifiedAgain = '<code> <div> blabla </div> </code>';
I'd recommend using a regular expression, e.g. preg_replace():
http://www.php.net/manual/en/function.preg-replace.php
http://www.webcheatsheet.com/php/regular_expressions.php
http://davebrooks.wordpress.com/2009/04/22/php-preg_replace-some-useful-regular-expressions/
Edit: It appears that I haven't fully answered your question. There is no built-in PHP function to do what you want, but you can do find and replace with regular expressions or even simple expressions: str_replace, preg_replace

Categories