High,
I'm using this function
function special_chars_replace($string){
$result = preg_replace("/[&%\$##'\*:\/\"\[\]\{\};\(\)\|\\\=!\^\?`~.,\+-]/", "", $string);
return $result;
}
to delete all spaces in a css class name.
<?php echo special_chars_replace(strtolower(str_replace(" ","",$itemTags))); ?>
How do i preserve the first space before the name? So i can use it for a css class name. For example: class="tags tag01 tag02"
Just add the space before you echo the string:
<?php
echo " ".special_chars_replace(strtolower(str_replace(" ","",$itemTags)));
?>
You can use this
<?php echo implode(" ",explode(" ",$itemTags)); ?>
Regular expression is the most effective way to do this.
echo preg_replace(' +', ' ', $itemTags);
What this does is look for one or more spaces (that's what the + does), and replaces it with a single space.
Code typed from memory.
Related
[PHP]I have a variable for storing strings (a BIIGGG page source code as string), I want to echo only interesting strings (that I need to extract to use in a project, dozens of them), and they are inside the quotation marks of the tag
but I just want to capture the values that start with the letter: N (news)
[<a href="/news7044449/exclusive_news_sunday_"]
<a href="/n[ews7044449/exclusive_news_sunday_]"
that is, I think you will have to work with match using: [a href="/n]
how to do that to define that the echo will delete all the texts of the variable, showing only:
note that there are other hrefs tags with values that start with other letters, such as the letter 'P' : href="/profiles... (This does not interest me.)
$string = '</div><span class="news-hd-mark">HD</span></div><p>exclusive_news_sunday_</p><p class="metadata"><span class="bg">Czech AV<span class="mobile-hide"> - 5.4M Views</span>
- <span class="duration">7 min</span></span></p></div><script>xv.thumbs.preparenews(7044449);</script>
<div id="news_31720715" class="thumb-block "><div class="thumb-inside"><div class="thumb"><a href="/news31720715/my_sister_running_every_single_morning"><img src="https://static-hw.xnewss.com/img/lightbox/lightbox-blank.gif"';
I imagine something like this:
$removes_everything_except_values_from_the_href_tag_starting_with_the_letter_n = ('/something regex expresion I think /' or preg_match, substring?);
echo $string = str_replace($removes_everything_except_values_from_the_href_tag_starting_with_the_letter_n,'',$string);
expected output: /news7044449/exclusive_news_sunday_
NOTE: it is not essential to be through a variable, it can be from a .txt file the place where the extracts will be extracted, and not necessarily a variable.
thanks.
I believe this will help her.
<?php
$source = file_get_contents("code.html");
preg_match_all("/<a href=\"(\/n(?:.+?))\"[^>]*>/", $source, $results);
var_export( end($results) );
Step by Step Regex:
Regex Demo
Regex Debugger
To get just the links out of the $results array from Valdeir's answer:
foreach ($results as $r) {
echo $r;
// alt: to display them with an HTML break tag after each one
echo $r."<br>\n";
}
How can I remove special characters like ;lt ;gt but not Anchor tag
e.g
<a href="http://www.imdb.com/name/nm0005069/">Spike Jonze</a> This cause by <a class="primary-black" href="http://example.com/community/RobHallums">RobHallums</a>
should be
Spike Jonze This cause by <a class="primary-black" href="http://example.com/community/RobHallums">RobHallums</a>
Here's a quick one for you:
<?php
// SET OUR DEFAULT STRING
$string = '<a href="http://w...content-available-to-author-only...b.com/name/nm0005069/">Spike Jonze</a> This cause by <a class="primary-black" href="http://e...content-available-to-author-only...e.com/community/RobHallums">RobHallums</a>';
// USE PREG_REPLACE TO STRIP OUT THE STUFF WE DON'T WANT
$string = preg_replace('~<.*?>~', '', $string);
// PRINT OUT OUR NEW STRING
print $string;
All I'm doing here is looking for <, followed by any character ., any number of times *, until it matches the next part of the string ?, which is >.
Any time it finds that, it replaces it with nothing. So you're left with the text you want.
Here is a working demo:
http://ideone.com/uSnY0b
use html_entity_decode:
<?php $url = html_entity_decode('<a href="http://www.imdb.com/name/nm0005069/">Spike Jonze</a>');
echo $url;
?>
the output will be:
Spike Jonze
EDIT:
<?php
preg_match_all('/<a .*?>(.*?)<\/a>/',$url,$matches);
//For Text Name
echo $matches[1][0]; //output : Spike Jonze
?>
I have a string that looks like this
$t="<b>vist</b>thank you for the follow.";
I am trying to remove the tag b and put an "#" instead of this tag.
I tried this
str_replace("<b></b>","#",$t);
but it doesn't replace the closing tag.
I don't know why it is not working may be there is something omitted in the code.
Try with
$search = array('<b>','</b>');
$replace = '#';
echo str_replace($search, $replace, $t);
To replace multiple words using str_replace() function,
You can Try this
$t="<b>vist</b> thank you for the follow";
$pattern=array();
$pattern[0]="<b>";
$pattern[1]="</b>";
$replacement=array();
$replacement[0]="#";
$replacement[1]="";
echo str_replace($pattern,$replacement,$t);
View the Demo
Try with -
$t="<b>vist</b>";
echo str_replace(array("<b>", "</b>"),"#",$t);
i want to repeat special character or TAB.
i try with str_repeat(str, int)
//my function repeat
function tab($num){
return str_repeat(' ', $num);
}
//if I call
echo '<table>' . "\r\n";
echo tab(3) . '<tr>';
//the result
<table>
	<tr>;
I have tried several ways with single quote and double quote,
but the results is always wrong
Use \t for printing tab
function tab($num){
return str_repeat('\t', $num);
}
function tab($num){
return str_repeat(html_entity_decode(' '), $num);
}
If you want to indent source code of your page, you should use "\t" instead of special characters.
function tab($num){
return str_repeat('\t', $num);
}
Otherwise if you need to indent text on your page (which user would see), add after tabs.
<span> </span> some text
Anyway your should use MVC instead of such things.
Can I store a special character inside a variable?
I mean something like:
<?php
$variable=' '
Then can i use something like echo $variable?
Yes, but don't take my word for it:
#!/path/to/php
<?
$variable = ' ';
echo $variable;
?>