php regex replace within results - php

I want to run over a css file and replace some values.
I want the replace to take place only within the Braces.
for example lets say we have the next css:
.redColor{color:red;padding-right:45px;/*etc....*/}
and I want to replace all the red values with blue.
I had tried to use the next code :
preg_replace("/{(.*)red(.*)}/","blue",$cssString)
but the result where:
.redcolorblue I want it to replace just the red only if it's withing braces and avoid the pattern around it...
the expected result should be:
.redColor{color:blue ;padding-right:45px;/*etc....*/}
This just an example for what I am trying to do, I want to change the css file itself, and change a lot of values inside it.
some clarifications
I want to do this replace in a CSS file, so I am loading the whole file into a variable and doing the replace, so solutions that replace only one value are not what I amlooking for

preg_replace('/(\{.*?)red(.*?\})/s', '$1blue$2', $cssString);

Try this:
preg_replace("/({.*)red(.*})/","${1}blue${2}",$cssString);
By using parentheses matching string is saved and can be referenced in replacement string as $1.
More details in http://php.net/preg_replace

Related

php line Smarty template engine reverse stristr

I want to edit my php template, but I can not get it right.
I use the the Smarty template engine.
My default php line looks like this:
{$childItem->getLabel()}
That does display HTML like this:
MY TEXT - MY SUB TEXT
I want to remove all content before the -. So that it only displays the MY SUB TEXT part.
I tried this, but that does not work, because that removes the content after the sign:
{$childItem->getLabel()|stristr:'-' : true}
How can I fix this?
The additional "true" parameter you're passing to stristr refers to $before_needle, so you'll get back the portion of the string before the hyphen. Unfortunately, removing that will get you back the portion of the string after and including the hyphen, i.e. "- MY SUB TEXT".
A clean solution would be to use explode, and then use the second element of the resulting array, like this:
{assign var="splitItem" value=" - "|explode:$childItem->getLabel()}
{$splitItem[1]}
As for php documentation for stristr:
If TRUE, stristr() returns the part of the haystack before the first
occurrence of the needle (excluding needle).
however, as the normal behaviour of the function is to include the needle and the text after it and that's not exactly what you want, you'll also have to remove it with replace, so:
{$childItem->getLabel()|stristr:'-'|replace:'-':''}

PHP/ Markdown: Need to match code blocks inside backticks

I am creating a developers site with code views,
but when they type code i need to it a certain way so in the php and can convert it to html a special way,
The markdown i need to be able to work with is
```codechanger
``php
<?php ?>
``
``c#
$foo = bar;
``
```
So once the first regex select code changer I then need to be able to select each code but i just need to know the regex to match everything inside there even if it is on multiple lines.
This is what i was trying to use
preg_match_all("/\`\`codechanger.*?^\`\`[^\r\n]*/s", $text, $out);
Any questions feel free to ask.
If you want to get the text between a start and end point, you can do something like this: START .*? END
```codechanger(.*?)```
When combined with the s flag like you have, that will give you everything in $1 between the starting and ending positions.
Once you have the results you can do another preg_match_all to get the inner tags.
Here is a demo

How to get the number out of a HTML string without tags?

I have the following string inside the source of some website:
user_count: <b>5.122.512</b>
Is this possible to get the number out of this string, even if the tags around this number were different? I mean, "user_count:" part won't change, but the tags can be changed, to strong for example. Or the tags could be doubled, or whatever.
How can I do that?
You can use
user_count:\s*<.*?>(.*?)<.*?>
See DEMO
I'd imagine you have to use JS to extract the content between the tags <b>5.122.512<b> from the DOM.
If you can assign an ID to this you can probably use document.getElementById('NAME_OF_YOUR_ID').innerHTML; to extract the number between it. If you need to process this inside a PHP script, you would probably need to POST this back to the server.
There are a couple of ways to get the number out of the string. One would be just to strip the tags and run a regular expression.
$s = "user_count: <b>5.122.512</b>"
preg_match_all("#user_count: (.+)#", strip_tags($s), $matches);
print_r($matches)
$matches[1] should match the number.

replacing single break tags for two using regex

Whilst being aware of the pitfalls/dangers of certain html manipulation with regex (instead of using say the PHP dom manipulator) I'm trying to achieve something that should be pretty simple and not that risky.
Basically I have some uncleaned html copy from a database that doesn't use paragraphs but line break tags to produce the effects of paragraphs. Sometimes though the user only entered content with a single break so that the text line returns but without a blank line appearing. In such instances and ONLY in such instances I want to replace that single <br> with two (<br><br>).
So as an example...
This is <br>a test<br><br>example!
would become
This is <br><br>a test<br><br>example!
Note how the second set of breaks is left alone as its already got 2 tags.
Simply replace one or more occurences of <br> with <br> :)
Replace what:
(<br>)+
Replace with:
<br><br>
You can use negative lookahead and lookbehind to solve this:
(?<!<br>)<br>(?!<br>)
See the example here: http://rubular.com/r/WYjoenH1SA
(?<!NOPREFIX)
(?!NOPOSTFIX)
The first part prevents from matching, if the NOPREFIX is present - the second one if NOPOSTFIX is present.

How to find text in webpage through PHP?

I have a pure text file without any HTML formatting. I want to search for a word in it? how should i do that? and I also want the next words before a comma. can i do that to?
so means:
if its: "word1":"i like stackoverflow", next thing
i want to find word1 which is in inverted commas and then i want the whole phrase i like stackoverflow without the inverted commas. and it should stop at that point.
is there any way to do that?
thanks...
Use a regular expression. In PHP, you can use a function like preg_match to do this.
For the pattern you described, your regular expression code could be something like:
$match_array = array();
$string_to_match = // load the string from a file and put it in this variable
preg_match('/"([A-Za-z0-9\s]+?)":"([A-Za-z0-9\s]+?)"/', $string_to_match, $match_array);
The results of the match will then be placed in $match_array.

Categories