RegEx teaser - php

Let's say we have 2 php variables:
$name = 'caption';
$url = 'http://domain.com/photo.jpg';
The input string of '{#url,<img src="," alt="{#name}" />}' should return:
'<img src="http://domain.com/photo.jpg" alt="caption" />'
The {tag} takes up to 3 parameters: {#variable[,text_before][,text_after]}.
What regex would be needed to make this happen? The tricky part is that a {#..} tag is nested within another.

I think you've come across one of those situations where you shouldn't use regex.
much like this one.
Multi-Line group and search with Regex
Multi-Line group and search with Regex

It's for a CMS. Admins can add column fields, then add template code for how it will be displayed on the listing page. The {#tags} are used to output the dynamic column values. This template code:
<p>Link: {#name} - {#date}</p>
would create a listing page like:
Link: link one - 2 July 2008
Link: link two - 14 June 2008
Link: link three - 9 February 2007
...
I figured that people might want to use column values within others, hence the "alt" tag example from the first post. So using regex for this would be a bad idea?

Related

How to find all class names or ids from a CSS file that contains a background image using Regular expression and PHP?

I want to get the class name or the ID from a text using regular expression with PHP.
For Instance:
.vc_custom_1547091583528{
margin-bottom:40px!important;
padding-top:7rem!important;
padding-bottom:1rem!important;
background:#092746 url(http://icoachu.us/wp-
content/uploads/2016/12/princeton.jpg?id=957)!important;
background-position:center!important;
background-repeat:no-repeat!important;
background-size:cover!important
}
This is css and the output should be:
array(0=>".vc_custom_1547091583528");
and also
array(0 => "http://icoachu.us/wp-content/uploads/2016/12/princeton.jpg?id=957")
So they will be two different functions.
Finally I have found the right answer. The regular expression can be made more smarter and short but right now this one works just fine.
([.#][a-zA-Z0-9_-]*?){[a-zA-Z0-9~;:#! _-]*\(['"]?(.*?)['"]?\)[a-zA-Z0-9~;:#! _-]*}

PHP extract String after pattern

I want to cut a html link with php.
The html link is everytime the same pattern
domain.com/forum/members/84564-name.html
I want to get the 84564 from the name.
the /forum/members/ is everytime the same.
and the "-" after the user-id is also everstime the same.
Can you help me to extract the user id?
I'm going to assume the user id may not always be 5 digits.
$domainSplit = explode("/", $theDomain); // split into parts
$theIdSplit=explode("-", $domainSplit[3]); // split 84564-name.html
$id=$theIdSplit[0];

RegEx for string replacement in MySQL Database

I have recently upgrade an IPB version of my forum but the quotes were not upgraded and IPS is not giving the support I need.
I need to build a regular expression do find and replace
For example this is the old forum format:
<div class="quotetop">QUOTE(Cleber__v # Apr 14 2015, 12:25 PM)
<a href="index.php?act=findpost&pid=2778161"><{POST_SNAPBACK}>
</a></div><div class="quotemain"><!--quotec-->
TEXT TO BE KEPT
<!--QuoteEnd-->
</div><!--QuoteEEnd-->
And this is the new format it should be on:
`<div>
<blockquote class="ipsQuote"
data-cite="Em 14/04/2015, (Cleber__v disse:" data-ipsquote=""
data-ipsquote-timestamp="1428004301" data-ipsquote-userid="2350"
data-ipsquote-username="Cleber__v" data-ipsquote-contapp="forums"
data-ipsquote-contenttype="forums" data-ipsquote-contentclass="forums_Topic"
data-ipsquote-contentid="105179" data-ipsquote-contentcommentid="2768819">
TEXT TO BE INSERTED</blockquote></div><p><span>​</span></p>`
So I need to find the content, save Username, postID (or commentid), time and text and replace it with the correct format.
I've been researching regex for about a week now with no sucess on how to make this happen
Anybody could help? Thank you
You should try for youself and give us the regex you tried, but here is a first step for you :
.*?>QUOTE\((?P<name>.*)\ \#\ (?P<date>[^\)]+).*?pid\=(?P<pid>[0-9]*).*?\<\!\-\-quotec\-\-\>(?P<text>.*?)\<\!\-\-QuoteEnd\-\-\>
See here how it works : https://regex101.com/r/dM0eG3/1 and how the match information corresponds to your need.
NB : you must remove all new line characters before applying this regex, but this is fairly easy to do in PHP or in any language you might use to create your db upgrade script.
That will extract all the relevant information from your text. Replacing these in the new format is left as an exercise for the reader.

Smarty dynamic variable issue

Unfortunately, in my current workplace, I have to use Smarty on a project they had before i was employed.
Anyway, I am trying to call in a Dynamic html title for each category in the app.
So for example, the title used to be pageTitle="{$category} in {$areaname}".
However I now want it to be: pageTitle="{html_title}". Within html_title (from db), is a string, for example: "Monkeys in the {areaname}" Where {areaname} could be "jungle".
When I output the result, I get:
"Monkeys in {areaname}".
So to cut a long story short, its not recognizing the variable. It is treating it as a string. I have googled my head off and can't find an answer. I hate smarty!
Please help!
Couldn't you just do something like
$html_title = preg_replace("/{areaname}/", "jungle", $html_title);
after fetching the title from the database?

Find and replace problem

My website, has 2 database tables. 1 of them have the posts_table and the other one have the videos.
At the moment i am getting the text images etc , normally from the post_table table.
In my CMS when we add a video there is added a short code
[media id=487 width=660 height=440]
This shortcode automaticly get the link of a video from the vid_table where the id is the same as the shortcode.
So what i want is:
I need to do the same thing that the short code do, when a video is added on CMS the short code is showed in the post, i need to delete the shortcode and instead of it want to be played a video that has the link on the vid_table.
I have some problems with my english , so if you dont understand again please tell me.
Any kind of help will be great.
Thank you.
EDITED: So i want to replace the whole media tag with a flash player, that plays the url that belongs to the ID in the media tag
BUMP !! CAN HELP PLEASE ?
This is quite a sophisticated problem actually. I was bored and made a basic tag parser. Right now it has some problems:
HTML rendering should be implemented in a separated class (and a template engine such as Twig should do the rendering);
Tag parsing is way too naive and will probably give you unexpected results if a tag's syntax is incorrect;
[media] tag does not support IE. You would have to change the source itself (method TagParser::renderMedia())
Some features to note:
extra parameters will be rendered as attributes for [link] tag, e.g [link id=25 class=foo] will output example.
parameters may contain spaces if you quote them: [link id=25 class="foo bar"] will output example
If DataProvider::findById() does not return a 'content' in its array, the parser will output http://example.com
The code is too long to paste here, you can find it on gist. Just put each file in the directory specified by the first commented line and you should be set. Run example.php to see it in action. You can find out some more details about using this script by looking at the unit test.
What do you want exactly?, you can get media id out of the text using
$text = 'some stuff [media id=468 width=660 height=440] more stuff';
preg_match("/media id=(.*) w/",$text, $results);
$result = $results[0];
$result = str_replace("media id=","",$result);
$result = str_replace("w","",$result);
$id = $result;

Categories