How to lowercase only HTML elements - php

How can I lowercase all HTML elements but not their attributes or attribute values?
I found a lot of examples like
$newString = preg_replace("/<[^>]+>/e", "strtolower('\\0')", $oldString);
But it lower cases everything, not just the tags.

Try the following:
$newString = preg_replace( "/<([^> ]+)/e", "strtolower('\\0')", $oldString )

how about:
$oldString = "<H1 CLASS='abc'>HGKJHG</H1>\n< P ClAsS='AbC123'>HKJGHG</P>";
$newString = preg_replace("~(</?\s*\w+)~e", "strtolower('\\1')", $oldString);
echo $newString,"\n";
output:
<h1 CLASS='abc'>HGKJHG</h1>
< p ClAsS='AbC123'>HKJGHG</p>

$newString = preg_replace("/</?\w+/e", "strtolower('\\0')", $oldString);

function change_case_tags($string, $action = 'mb_strtolower')
{
$string = preg_replace('!<([^> ]+)!e', "$action('\\0')", $string);
if(strpos($string, '=') !== false)
{
return $string = preg_replace('!(?:\s([^\s]+)=([\'"])?(.+)\\2)!Uie', "$action(' \\1').'='.str_replace('\\\\\\', '', '\\2').'\\3'.str_replace('\\\\\\', '', '\\2').''", $string);
}
return $string;
}

Related

How to remove "<>" brackets from string in php?

$cont=htmlspecialchars(file_get_contents("https://myanimelist.net/anime/30276/One_Punch_Man"));
function getBetween($string, $start = "", $end = ""){
if (strpos($string, $start)) { // required if $start not exist in $string
$startCharCount = strpos($string, $start) + strlen($start);
$firstSubStr = substr($string, $startCharCount, strlen($string));
$endCharCount = strpos($firstSubStr, $end);
if ($endCharCount == 0) {
$endCharCount = strlen($firstSubStr);
}
return substr($firstSubStr, 0, $endCharCount);
} else {
return '';
}
}
$name=getBetween($cont,'title',' - MyAnimeList.net');
//$name=preg_replace('/[^a-zA-Z0-9 \p{L}]/m', '', $name);
preg_replace('/(*UTF8)[\>\<]/m', '', $name);
trim($name," ");
//$name=str_replace("gt", "", $name);
echo $name;
i want to find the text between title tags. how to do this?
for example in this page title contains 'One Punch Man - MyAnimeList.net' i want to get that
Just use string replace function:
$string = '<BoomBox>';
$string = str_replace('<', '', $string);
$string = str_replace('>', '', $string);
echo $string; // output: Boombox
http://php.net/manual/en/function.str-replace.php
You edited your answer, and we can now see you are dealing with XML/HTML. It's always better to work with the DOM classes. Never use regex! There is a famous Stack Overflow post explaining why never to parse html with regex. Try this solution instead:
<?php
$dom = new DOMDocument();
$dom->loadHTML('<title>BoomBox</title>');
echo $dom->getElementsByTagName('title')->item(0)->textContent;
http://php.net/manual/en/class.domdocument.php
http://php.net/manual/en/class.domnode.php
See it working here https://3v4l.org/EjPQd
You can use preg_replace();, or strip_tags();.
Example preg_replace();:
$str = '> One Punch Man';
$new = preg_replace('/[^a-zA-Z0-9 \p{L}]/m', '', $str);
echo $new;
Output: One Punch Man
Above example will only allow a-z, A-Z and 0-9. You can expand this.
Example strip_tags();:
$str = '<title> BoomBox </title>';
$another = strip_tags($str);
echo $another;
Output: BoomBox
Documentation:
http://php.net/manual/en/function.preg-replace.php // preg_replace();
http://php.net/manual/en/function.strip-tags.php // strip_tags();
You can also use a single call to str_replace with the ['<','>'] as the search argument:
$string = '<BoomBox>';
echo str_replace(['<', '>'], '', $string) . PHP_EOL;
// => Boombox
Or, you may use a regex with preg_replace (especially, if you plan on adding more restrictions for in-context matching to it):
echo preg_replace('~[<>]~', '', $string);
// => Boombox
See the PHP demo.

Regular expression for finding multiple patterns from a given string

I am using regular expression for getting multiple patterns from a given string.
Here, I will explain you clearly.
$string = "about us";
$newtag = preg_replace("/ /", "_", $string);
print_r($newtag);
The above is my code.
Here, i am finding the space in a word and replacing the space with the special character what ever i need, right??
Now, I need a regular expression that gives me patterns like
about_us, about-us, aboutus as output if i give about us as input.
Is this possible to do.
Please help me in that.
Thanks in advance!
And finally, my answer is
$string = "contact_us";
$a = array('-','_',' ');
foreach($a as $b){
if(strpos($string,$b)){
$separators = array('-','_','',' ');
$outputs = array();
foreach ($separators as $sep) {
$outputs[] = preg_replace("/".$b."/", $sep, $string);
}
print_r($outputs);
}
}
exit;
You need to do a loop to handle multiple possible outputs :
$separators = array('-','_','');
$string = "about us";
$outputs = array();
foreach ($separators as $sep) {
$outputs[] = preg_replace("/ /", $sep, $string);
}
print_r($outputs);
You can try without regex:
$string = 'about us';
$specialChar = '-'; // or any other
$newtag = implode($specialChar, explode(' ', $string));
If you put special characters into an array:
$specialChars = array('_', '-', '');
$newtags = array();
foreach ($specialChars as $specialChar) {
$newtags[] = implode($specialChar, explode(' ', $string));
}
Also you can use just str_replace()
foreach ($specialChars as $specialChar) {
$newtags[] = str_replace(' ', $specialChar, $string);
}
Not knowing exactly what you want to do I expect that you might want to replace any occurrence of a non-word (1 or more times) with a single dash.
e.g.
preg_replace('/\W+/', '-', $string);
If you just want to replace the space, use \s
<?php
$string = "about us";
$replacewith = "_";
$newtag = preg_replace("/\s/", $replacewith, $string);
print_r($newtag);
?>
I am not sure that regexes are the good tool for that. However you can simply define this kind of function:
function rep($str) {
return array( strtr($str, ' ', '_'),
strtr($str, ' ', '-'),
str_replace(' ', '', $str) );
}
$result = rep('about us');
print_r($result);
Matches any character that is not a word character
$string = "about us";
$newtag = preg_replace("/(\W)/g", "_", $string);
print_r($newtag);
in case its just that... you would get problems if it's a longer string :)

How to remove unwanted commas from a string?

How can i remove duplicate commas used in string.
String = ",a,b,c,,,d,,"
I tried rtrim and itrim functions and removed the unwanted commas from beginning and ending .How can i remove duplicate commas ?
Try this:
$str = preg_replace('/,{2,}/', ',', trim($str, ','));
The trim will remove starting and trailing commas, while the preg_replace will remove duplicate ones.
See it in action!
Also, as #Spudley suggested, the regex /,{2,}/ could be replaced with /,,+/ and it would work too.
EDIT:
If there are spaces between commas, you may try adding the following line after the one above:
$str = implode(',', array_map('trim', explode(',', $str)))
i think you can just explode your string and then create a new one getting only relevant data
$string = ",a,b,c,,,d,,";
$str = explode(",", $string);
$string_new = '';
foreach($str as $data)
{
if(!empty($data))
{
$string_new .= $data. ',';
}
}
echo substr_replace($string_new, '', -1);
This will output
a,b,c,d
Live Demo
EDITED
If you are having problems with blank spaces you can try use this code
$string = ",a,b,c, ,,d,,";
$str = explode(",", str_replace(' ', '', $string));
$string_new = '';
foreach($str as $data)
{
if(!empty($data))
{
$string_new .= $data. ',';
}
}
echo substr_replace($string_new, '', -1);
This should solve spaces issue
Probably not very fast, but a simple method may be:
$str = "a,b,c,,,d";
$str2 = "";
while($str <> $str2) {
$str2 = $str;
$str = str_replace(',,', ',', $str);
}
<?php
$str = ",a,b,c,,,d,,"
echo $str=str_replace(',,','',$str);
?>
Output:
,a,b,c,d
<?php
$str = ",a,b,c,,,d,,"
echo $str=trim(str_replace(',,','',$str),',');
?>
Output:
a,b,c,d

How could replace from the PHP String

I wan't to replace
[a href='url']link[/a]
to
<a href='url'>link</a>
I am using $line = str_replace("[a href='+(.*)+']", "<a href='+(.*)+' >", $line); is not working.
Why not just use:
$search = array('[', ']');
$replace = array('<', '>');
$line = str_replace($search, $replace, $line);
You have to use a regular expression to do this
$line = preg_replace('~\\[a +href=\'([^\']+)\'\\]([^\\[]+)\\[/a\\]~', '$2', $line);
simply use
$string = str_replace(array('[', ']'), array('<', '>'), $string);
This is a great tutorial http://www.youtube.com/watch?v=x9VLWlQhNtM it shows you how to make a small templating engine and it covers what your asking
Try this :
$str = "[a href='url']link[/a]";
$new_str = preg_replace('/\[a href=\'(.*)\'\](.*)\[\/a\]/','<a href=\'$1\'>$2</a>',$str);
echo $new_str;

how to remove html comments in php

I am trying to remove any comments embedded with the html file
$data= file_get_contents($stream); <br>
$data = preg_replace('<!--*-->', '', $data); <br>
echo $data;
I am still ending up with all the comments < !- bla bla bla -->
What am I doing wrong?
// Remove unwanted HTML comments
function remove_html_comments($content = '') {
return preg_replace('/<!--(.|\s)*?-->/', '', $content);
}
As you can read here : https://davidwalsh.name/remove-html-comments-php
I know lots of answers are already posted. I have tried many but for me this regular expression works for multi line (in my case 40 line of comments) HTML comments removal.
$string = preg_replace("~<!--(.*?)-->~s", "", $string);
Cheers :)
The below regex will remove HTML comments, but will keep conditional comments.
<!--(?!<!)[^\[>].*?-->
You could do it without using regular expression:
function strip_comments($html)
{
$html = str_replace(array("\r\n<!--", "\n<!--"), "<!--", $html);
while(($pos = strpos($html, "<!--")) !== false)
{
if(($_pos = strpos($html, "-->", $pos)) === false)
$html = substr($html, 0, $pos);
else
$html = substr($html, 0, $pos) . substr($html, $_pos+3);
}
return $html;
}
s/<!--[^>]*?-->//g
switch up regular expression
Regular expressions are very difficult to corral into doing what you want here.
To match arbitrary text in a regex, you need .*, not just *. Your expression is looking for <!-, followed by zero or more - characters, followed by -->.
I would not use regex for such a task. Regex can fail for unexpected characters.
Instead, I would do something that is safe, like this:
$linesExploded = explode('-->', $html);
foreach ($linesExploded as &$line) {
if (($pos = strpos($line, '<!--')) !== false) {
$line = substr($line, 0, $pos);
}
}
$html = implode('', $linesExploded);
You should do this way:
$str = "<html><!-- this is a commment -->OK</html>";
$str2 = preg_replace('/<!--.*-->/s', '', $str);
var_dump($str2);

Categories