I am parsing some articles from a database with php and in the articles there are links which I would like to overwrite. Link always start with "http://cdn.example.com/" and the end parser is htmlspecialchars_decode($item->parse_articles(), ENT_NOQUOTES).
So before the articles are passed to the HTML DOM, I would like to replace all those href's that contain (?) example.com or maybe even if faster and possible to remove the <a> completely.
.
How is this possible? and if possible, is this considered faster option than passing it first to the DOM and manipulating it from there on the client-side?
You could try something like the following in PHP:
$newtext = preg_replace('/^("http:\/\/cdn\.example\.com\/){1}(.*)("){1}$/', '"#" class="disabled-link"', $oldtext);
$oldtext being your input article as a string.
$newtext being the text to echo on the page.
Broken down:
Find text starting with "http://cdn.example.com/
Then match anything
Stop at "
Replace with "#" class="disabled-link"
This should let you remove the link and also I added the class part so that you can add some CSS to style the links as text.
Example:
.disabled-link{
color:#000;
pointer-events: none;
cursor: default;
text-decoration: none;
}
All this combined will provide users with a link that is completely invisible without looking into the DOM or the source.
Related
There's a simple way to include when there's a new line in the database (nl2br), but is there anything similar for tabs?
I've tried different solutions which works in the way of the look, but not when you're copying the code. I've tried a CSS style and made it like:
#br{
margin-right: 30px;
float: left;
}
But once I copy the code, there's no tab. In my database there's a TAB, but how do I print the tab?
You can use a bit of CSS to display tabs as tabs.
#br {
white-space: pre-wrap;
tab-size: 4;
}
pre-wrap is the best solution, I think, because it still allows the text to wrap normally when a line is full. pre is also possible, but then the text won't break at the end of a line.
tab-size is optional. By default it is set to 8 spaces, but you can change that by specifying a number of spaces in this property.
Note that I've copied your CSS selector, #br, but normally I would make a class for this, so you can easily apply this style to any number of elements in your page.
Also note, since pre-wrap also displays line breaks as actual breaks, you probably won't need to call nl2br on the server anymore.
You can write a function like nl2br(). Something like:
<?php
function tab2span($str){
if(strpos($str, "\t"){
$str = str_replace('\t','<span class="tabbed"> </span>',$str);
}
return $str;
}
?>
Then you can also adjust your CSS to style it better if you need.
I have this text in mysql adding even directly but do not want to lose the labels only the styles and formats that tenien
<p style="margin-bottom: 20px; padding: 0px; border: 0px;"><span style="line-height: 1.428571429;">Allí, el club crema</span><p>
use strip_tags but removes the entire label
strip_tags ($ data, "<p>");
I want it that way:
<p>Allí, el club crema<p>
I hope your help, thank you very much beforehand for your answers
Warning, anti-pattern: using REGEX on mark-up is generally a bad idea. However it's sometimes more convenient, so to hell with it:
$data = preg_replace('/(<\w+) [^>]+/', '$1', $data);
There is no php function for that. The strip tags function will strip the tag completely, and allowing a tag will keep the tag in place, including the attributes. You'll need to load the html in a xml parser and reconstruct the output, or, and I would advise you to go that way, use regex to strip out any html attributes (after you've stripped away the tags you don't need anyway. See also this question
I'm trying to add CSS styling to all hyperlinks unless it has a "donttouch" attribute.
E.g.
Style this: style me
Don't style this: <a href="http://whatever.com" donttouch>don't style me</a>
Here's my preg_replace without the "donttouch" exclusion, which works fine.
preg_replace('/<a(.*?)href="([^"]*)"(.*?)>(.*?)<\/a>/','<a$1href="$2"$3><span style="color:%link_color%; text-decoration:underline;">$4</span></a>', $this->html)
I've looked all over the place, and would appreciate any help.
Find (works also in Notepad++)
(?s)(<a (?:(?!donttouch)[^>])+>)(.*?)</a>
Replace with (Replace all in Notepad++):
\1<span style="whatever">\2</span></a>
This can be accomplished without a regular expression. Instead, use a CSS attribute selector.
For example, use these rules:
a { font-weight: bold; color: green }
a[donttouch=''] { font-weight: normal; color: blue }
Technically, you are styling the elements with the 'donttouch' attribute, but you can use default values. This will be more efficient than attempting to use a regular expression to parse your HTML, which is usually a bad idea.
I am reading a string my user is inputting via PHP and I need to spit it back out in a div tag. This div tag has a width of 500px. If the user enters a word that is too long, the word will overflow the container. If the user enters two words that are almost two long, it will split into two lines.
My question is how do I determine if a word is too long or not? I have tried setting a character count, which is not an accurate representation of length as certain characters (ie W and I) have different widths. Is there a solution?
My current algorithm is to break the user input into chunks, each of 40 characters, and output it.
If you want to still implement your character count mechanism you can; you just need to make sure your text is mono-spaced (same width). To do this you can just add <pre></pre> around your text block; this can also be accomplished with <code></code> and <tt></tt> but if you want a simple CSS solution you could use.
<style>
.myclass { word-wrap:break-word; }
</style>
<p class="myclass">some text</p>
Usually you shouldn't use PHP for things like that. Try CSS instead:
.break { word-wrap: break-word; }
will do the trick
you can simply break-up words that are larger than, say, 20 characters - into chunks, using the <wbr> tag. Here's some more info: http://motyar.blogspot.co.il/2011/07/tell-browser-they-may-break-your.html
I have some really messy HTML with lots of spans and other tags.
I'm trying to only keep <span style="font-weight: bold"> while removing the other such span tags.
I have this so far:
$content = strip_tags($content, '<br>,<quote>,<code>,<pre>,<ul>,<li>,<ol>,<span>');
I want to remove <span> because it adds all the other spans globally, I'd just want the spans with font-style in them. How can I do this?
strip_tags can't do this.
Take a look at HTML Purifier. It's designed exactly for this use case. You can give it a whitelist of tags and attributes to allow. It also has basic CSS parsing, allowing you to whitelist and blacklist CSS properties.
In this case, you'd probably do something like:
// This has not been tested, but should work
$configuration->set('HTML.Allowed', 'br,quote,code,pre,ul,li,ol,span[style]');
$configuration->set('CSS.AllowedProperties', 'font-weight');
Now, you're still going to be left with some extra span tags. You've suggested that you simply want them gone. This is going to be a bit stickier. You want to use a DOM manipulation tool to find each useless span, capture the contents, remove the span, then insert the contents where the span was. phpQuery was already pointed out, and Simple HTML DOM should also do the trick. PHP's own DOM extension can also do this, but it's going to be much more of a bear.