I got a Button, that creates this template code
[5afc4076e9f1b1526481014.pdf]##LINKNAME## (96.51 kb)
The filename and file size vary und the user can change the ##LINKNAME##, as well.
This code goes to a database and when I get it back, I want to replace it to
##LINKNAME## <i>(96.51 kb)</i>
I think I need to use preg_replace() but I am not really good at regular expressions.
I stopped here:
<?php
$string = ' [5afc4076e9f1b1526481014.pdf]##LINKNAME## (96.51 kb)';
$regex = '[[a-zA-Z0-9]+.pdf](.*?)\s';
$replace = 'I DONT KNOW';
echo preg_replace($regex, $replace, $string);
?>
I know that this is a complete mess, but I'm not getting any results as long as I don't know the regex and the correct $replace.
Regex: ^\[([^\]]+)\](\w+)\s\(([^)]+)\)$
Replace with: \2<i>(\3)</i>
Demo
Related
I have a shortcode which I want to be able to strip away depending on the context of the post. Eg.
[tooltip slug="test"]Test Text[/tooltip]
I would like the output to be:
<span class="dummy">Test Text</span>
I have experimented (a lot!) with preg_replace and I can't seem to get it to recognize that the replacement string is between the ']' and then delimited by '[/tooltip]' without doing multiple passes.
Ideas?
Update: As so often happens, about 10 seconds after I wrote this one of my attempts seemed to work. I don't think it's as good as the solution below but FWIW...
$my_var .= preg_replace('/(?:\[tooltip slug=\"([^\"]*)"[^\>]*\]([^\<]*)\[\/tooltip\])/', '<span class="dummy">\\2</span>', $my_post->post_content);
Here is the simple regex you are looking for.
$result = preg_replace('%\[tooltip slug="[^"]*"]([^[]*)\[/tooltip]%',
'<span class="dummy">\1</span>', $subject);
What we do here is capture the text between the tooltip tags, and insert it in the replacement.
Let me know if you need any details.
$test = preg_match('/\[([^\]]+)\]([^\[]+)\[/', '[tooltip slug="test"]Test Text[/tooltip]', $matches);
echo $matches[2];
The company I work for have asked me to give them the ability to place a modal box on the web page from the CMS, but do not want to type HTML. As I cannot for the life of me understand regex I can't get it.
The layout of the code they should type is this:
++modal++
Some paragraph text.
Another paragraph.
++endmodal++
The paragraphs are already converted by markdown into <p>paragraph</p>.
So really the match has to be ++modal++ any number of A-Za-z0-9any symbol excluding + ++endmodal++ then replaced with HTML.
I'm not sure it preg_match or preg_replace should be used.
I got this far:
$string = '++modal++<p>Hello</p>++endmodal++';
$pattern = '/\+\+modal\+\+/';
preg_match($pattern, $string, $matches);
Thank you in advance.
EDIT: A to be a bit more clear, I wish to replace the ++modal++ and ++endmodal++ with HTML and leave the middle bit as is.
I don't really think you need a RegEx here as your delimiters remain always the same and always on the same position of the string. Regular expressions are also expensive on resources and as a third counter argument you said you're not fit with them.
So why not use a simple replacement or string trimming if it comes to that.
$search = array('++modal++', '++endmodal++');
$replacement = array('<tag>', '</tag>');
$str = '++modal++<p>Hello</p>++endmodal++';
$result = str_replace($search, $replacement, $str);
Where, of course, '<tag>' and '</tag>' are just example placeholders for your replacement.
This is what the manual for str_replace() says:
If you don't need fancy replacing rules (like regular expressions),
you should always use this function instead of preg_replace().
I think you should get your desired content using:
preg_match('/\+\+modal\+\+([^\+]+)\+\+endmodal\+\+/', $string, $matches)
$matches[1] = '<p>Hello</p>
You're trying to re-invent the wheel here. You're trying to write a simple template system here, but there are dozens of templating tools for PHP that you could use, ranging from big and complex like Smarty and Twig to really simple ones that aren't much more than you're trying to write.
I haven't used them all, so rather than recommend one I'll point you to a list of template engines you could try. You'll probably find more with a quick bit of googling.
If you do insist on writing your own, it's important to consider security. If you're outputting anything that contains data entered by your users, you must make sure all your output is properly escaped and sanitised for display on a web page; there a numerous common hacks that can take advantage of an insecure templating system to completely compromise a site.
<?php
$string = '++modal++<p>Hello</p>++endmodal++';
$patterns = array();
$patterns[0] = "/\+\+modal\+\+/"; // put '\' just before +
$patterns[1] = "/\+\+endmodal\+\+/";
$replacements = array();
$replacements[1] = '<html>';
$replacements[0] = '</html>';
echo preg_replace($patterns, $replacements, $string);
?>
Very similar to this example
I am attempting to use an RSS feed from Twitter (the ones that will be gone a year from now) with Feedburner but a line of code in the xml is causing a parsing error in feedburner. I think it's
xmlns:twitter="http://api.twitter.com"
I am looking for a way to give this information to feedburner in a way that it likes: namely php including the contents of the RSS xml page in an xml on my own site (with the offending string removed). Including the xml contents is a piece of cake - all I need is a piece of code to say iff string fragment == xmlns:twitter="http://api.twitter.com" then string frament = "".
Anybody any idea of the syntax for this?
You can try with str_replace:
str_replace('xmlns:twitter="http://api.twitter.com"', '', $string);
Also, you can try to urlencode http://api.twitter.com.
Alternatively, you could use preg_replace function:
http://php.net/manual/en/function.preg-replace.php
<?php
$string = 'string to be searched';
$pattern = 'a particular pattern';
$replacement = ''; //deletion
echo preg_replace($pattern, $replacement, $string);
?>
Lets begin with an example. Say in a file file.php following texts exist.
This is just to show example. This is just to show example. This is just to show example. This is just to show example. end. Okay This is just to show example. This is just to show example.
Now in the above text if i find okay then i will remove the rest of the content including the word okay and save the file in the server.
Someone please show me an efficient way to do this.
You could also use explode to split the content. Depending on how extensive your problem is this may not be the best solution, but I thought I would just include it also:
$string = $your_file_content
$split = explode('okay', strtolower($string));
//$split[0] = everything before 'okay';
//$split[1] = everything after 'okay';
There's a bunch of different ways you can do this strpos, regex. Here's an example
$str = 'This is just to show example. This is just to show example. This is just to show example. This is just to show example. end. Okay This is just to show example. This is just to show example.';
preg_match('%Okay%', $str, $match);
if($match){
// save file to server
}
$pos=strpos ($inputstring, 'okay');//test to see if there is a match (it will find the first match)
if($pos!==false){//if there was a match
$cutstring=substr($inputstring, 0, $pos);//extract a substring from the begining to the match
//echo $cutstring
}
Read more about those functions here:
http://php.net/manual/en/function.substr.php
http://php.net/manual/en/function.strpos.php
$str = "This is just to show example. This is just to show example. This is just to show example. This is just to show example. end. Okay This is just to show example. This is just to show example.";
$fnd = "Okay";
if (false !== $p = stripos($str, $fnd)) $str = substr($str, 0, $p);
echo $str; // Now save it or whatever.
I wrote a little search script for a client, it works and words get highlited, BUT...
Imagine this situation:
search term: test
found result: Hello this is a test
In this example both 'test' in the href part and between the <a> tags get highlited, breaking the link.
How could I prevent this?
Edit:
So this is what I need: A regex replace function that replaces all matched search strings EXCEPT the ones that are located inside a href attribute
You can not parse XML with regular expressions. :( If you want a dirty regex solution that still works in many cases you may try this regex.
">[^<]*?(test)"
First you look for a tag closing brace and than you make sure that no other tag is opened in between.
Ideally you want to parse HTML and replace only the textual parts of it.
Got it!
$body = $row['body'];
$pattern = "/".$search_string."(?!([^<]+)?>)/i";
$replacement = "<strong class='highlite'>".$search_string."</strong>";
$altered_body = preg_replace($pattern, $replacement, $body);
print($altered_body);