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.
Related
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 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.
I'm currently making sort of a CSS minifier and well, I think examples for what I'm trying to achieve are the simplest way to explain.
I'm trying to transform this:
/* CSS Content */
.class{
text-align:center;
border-bottom:1px solid #ccc;
}
.anotherclass, .another, .another{
text-align:center;
border-bottom:1px solid #ccc;
}
Into:
.class{text-align:center;border-bottom:1px solid #ccc;}.anotherclass,.another,.another{text-align:center;border-bottom:1px solid #ccc;}
Thus: removing comments, unnecessary whitespace and new lines.
So far I got to remove the comments and new lines (Using one expression and a function exploding the string on \n, then appending the parts). The whitespaces are a bit more difficult, since the whitespaces within the {} should be removed but not between the colon and semicolon.
Since I'm quite inexperienced with the use of regular expressions, have no good reference book at hand and Google does'nt seem to have the answer: I'm wondering if anyone here can help me with creating one good Regex to accomplish this.
Thanks in advance!
If you must do it this way then this will minify the example you gave:
(/\*[^/]+\*/|^\t*|^\s*|\n|\s+(?=[\{.])|(?<=[\{;])\s+)
It assumes the flavour of regex you're using allows positive look-behinds
I guess you could try something like this:
(?<=[{};])\s+|\/\*.+?\*\/
http://rubular.com/r/djDtEPzEV0
Cleans out whitespace in front of parentheses, semicolons and all comments.
$TOPIC_CONTENT = preg_replace("!<code>(.+)</code>!is","<div style='color: #00FF00;
background-color: #000000; border-radius: 5px; margin: 5px;"<pre>".htmlspecialchars("$0")."</pre></div>",$TOPIC_INFO->content);
How can I get this to work? I have no idea how to pull this off, and I know my current way is invalid.
Use preg_replace_callback. Be a little careful with your regex .. I think you want to use .+? instead of just .+. The usual mantra is "don't parse html with regex," but for something as simple as this I don't see the harm.
Except for preg_replace_callback as in tandu's answer, you can also use the /e switch, and your replacement string will be *e*valuated as PHP code, and its result will be used.
I.e you could do:
preg_replace("!<code>(.+?)</code>!ise",
'"<pre style=\"color: #0f0; background: #000;\">" . htmlspecialchars("$1") . "</pre>"',
$string);
I'm wanting to scan through a css file and capture both comments and the css. I've came up with a regex that's almost there, however it's not quite perfect as it misses out properties with multiple declarations i.e.
ul.menu li a, # Won't capture this line
ul.nice-menu li a { text-decoration: none; cursor:pointer; }
Here's the regex that I'm working with:
(\/\*[^.]+\*\/\n+)?([\t]*[a-zA-Z0-9\.# -_:#]+[\t\s]*\{[^}]+\})
I've been testing this at rubular.com and here is what it currently matches, and what the array output is like.
Result 1
[0] /* Index */
/*
GENERAL
PAGE REGIONS
- Header bar region
- Navigation bar region
- Footer region
SECTION SPECIFIC
- Homepage
- News */
[1] html { background: #ddd; }
Result 2
[0]
[1] body { background: #FFF; font-family: "Arial", "Verdana", sans-serif; color: #545454;}
I must point out that I'm still a new when it comes to regular expressions, so if anyone can help and show where I'm going wrong, it'd be much appreciated :)
BTW:
I'm using PHP and preg_match_all
CSS cannot be fully parsed with a regex (see CSS Grammar: http://www.w3.org/TR/CSS2/grammar.html). The {...} can be split over lines, for example, and your current version wouldn't handle this. If you need to do this, you should read the CSS spec and use a tool like ANTLR to generate a parser.
Here is an example from the W3C spec (http://www.w3.org/TR/CSS2/syndata.html):
#import "subs.css";
#import "print-main.css" print;
#media print {
body { font-size: 10pt }
}
h1 {color: blue }
No normal regex is powerful enough to deal with nested {...} etc. let alone the contents of the imported stylesheets.
What language are you using?
You should probably just use a library to parse the CSS. Libraries can save you a lot of grief.