php line Smarty template engine reverse stristr - php

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:'-':''}

Related

Smarty strpos and substr to get text after last forward slash in URL

After lots of searching this is the closest I got. I'm using {$smarty.server.REQUEST_URI}
to get second part of the URL after http. Naturally am ending up with lots of text1/text2/text3. Now I'm after extracting last string of text located after the last forward slash.
from my understanding I have to use strpos to get the position of the last forwardslash, ideally start from the end and stop on the first slash which in reality would be the last since we started right-to-left, and then use substr to get anything from that strpos onwards.
You can do it this way (if you using Smarty2):
{assign var="dirs" value="/"|explode:$smarty.server.REQUEST_URI}
{math equation ="x-y" x=$dirs|#count y=1 assign="last"}
{$dirs[$last]}
and this way:
{assign var="dirs" value="/"|explode:$smarty.server.REQUEST_URI}
{$dirs[$dirs|#count-1]}
if you are using Smarty 3
The template is not the proper place for such processing. Parse the request string in PHP and assign to the template the values it needs to display. The template systems were invented to separate the logic (processing of data, done in the code) from the presentation (HTML and stuff, done in the templates).

php regex replace within results

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

Php regex match a string between two html tags with the tags been unknown

Ok, so here's my issue:
I have a link, say: http://www.blablabla.com/watch?v=1lyu1KKwC74&feature=list_other&playnext=1&list=AL94UKMTqg-9CfMhPFKXPXcvJ_j65v7UuV
And the link is between two tags say like this:
<br>http://www.blablabla.com/watch?v=1lyu1KKwC74&feature=list_other&playnext=1&list=AL94UKMTqg-9CfMhPFKXPXcvJ_j65v7UuV<br></p>
Using this regex with preg_replace:
'#(^|[^\/]|[^>])('.addcslashes($link,'.?+').')([^\w\/]|[^<]$)#i'
As such:
preg_replace('#(^|[^\/]|[^>])('.addcslashes($link,'.?+').')([^\w\/]|[^<]$)#i', "***",$strText);
The resulted string is :
<br***p>
Which is wrong!!
It should have been
<br>***<br></p>
How can I get the desired result? I have blasted my head out trying to solve this one out.
I would like to mention that str_replace replaces even the link within another valid link, so it's not a good method, I need an exact match between two boundaries, even if the boundary is text or another HTML tag.
Assuming you don't want to use a DOM parser for some reason, I believe doing what you intended is as simple as the following:
preg_replace('#(^|[^\/]|[^>])('.addcslashes($link,'.?+').')([^\w\/]|[^<]$)#i', "$1***$3",$strText);
This uses $1 and $3 to put back the delimiting text you matched in your regular expression.
As others have pointed out, using a DOM parser is more reliable.
Does this do what you want?

Replace anchor text with PHP (and regular expression)

I have a string that contains a lot of links and I would like to adjust them before they are printed to screen:
I have something like the following:
replace_this
and would like to end up with something like this
replace this
Normally I would just use something like:
echo str_replace("_"," ",$url);
In in this case I can't do that as the URL contains underscores so it breaks my links, the thought was that I could use regular expression to get around this.
Any ideas?
Here's the regex: <a(.+?)>.+?<\/a>.
What I'm doing is preserving the important dynamic stuff within the anchor tag, and and replacing it with the following function:
preg_replace('/<a(.+?)>.+?<\/a>/i',"<a$1>REPLACE</a>",$url);
This will cover most cases, but I suggest you review to make sure that nothing unexpected was missed or changed.
pattern = "/_(?=[^>]*<)/";
preg_replace($pattern,"",$url);
You can use this regular expression
(>(.*)<\s*/)
along with preg_replace_callback .
EDIT :
$replaced_text = preg_replace_callback('~(>(.*)<\s*/)~g','uscore_replace', $text);
function uscore_replace($matches){
return str_replace('_','',$matches[1]); //try this with 1 as index if it fails try 0, I am not entirely sure
}

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