Search the Community
Showing results for tags 'preg_match'.
-
I'm having a little trouble with preg_replace. The aim is to take a shortcode and replace it with some html code. The shortcode is of the format [image file=name.ext]. Each instance of the shortcode in some content should be replaced with <img src="path/to/images/name.ext" alt="name image" /> What is actually happening is the shortcode is being replaced with [<img src="path/to/images/name.ext" alt="name image" />] Printing out the relevant array shows no brackets so not sure where to look to solve this. Also can preg_match be used to find more than one occurrence of a pattern? The php manual indicates yes, but my testing was only coming up with one occurrence so I've resorted to using preg_match_all. Here's my test file with some content if you want to test it out. As ever please suggest any improvements. Thanks. define ('BASE_URL', '/path/to/root/'); $content = '<p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.</p> [image file=file1.jpg]<h3>Ipsem</h3> <p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.</p> [image file=file2.jpg]<h3>Lorem</h3> <p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. </span>ipsem lorem blah di blah.</span>'; $html_content=replace_shortcodes($content); echo( $html_content); function replace_shortcodes($content){ $html_array=array(); $shortcodes = get_all_shortcodes($content); foreach ($shortcodes as $shortcode) { $filename = get_sc_attr($shortcode); $alt = substr($filename, 0, strpos($filename,'.')); $html_array[]='<img src="' . BASE_URL . 'images/' . $filename . '" alt="'. $alt. ' image" />'; } $html_content= preg_replace($shortcodes, $html_array, $content); return $html_content; } function get_all_shortcodes($content){ $sc_regex = '/\[\s*image\s+file\s*=\s*[\w-]+\.[\w-]+\s*\]/'; preg_match_all($sc_regex, $content, $matches); return $matches[0]; } function get_sc_attr($shortcode) { $length = (strpos($shortcode,']') - strpos($shortcode,'=')); $filename = trim(substr($shortcode, strpos($shortcode,'=') +1,$length-1)); return $filename; }
-
I've been using a script to check submissions for proper email formatting. It looks like this: eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email) Note, that I didn't write this script, it was provided by someone else. It's worked fine for years, but now I'm getting an error message saying the function has been deprecated. I think my host must have installed a new version of PHP, 5.3.2. That's cool, I just need to figure out what to replace this code with. I've done a little research and it seems there are several ways to approach this, including: 1. just replace "eregi" with "preg_match." 2. use "filter_var" and "FILTER_VALIDATE_EMAIL" instead. Any advice as to what would be the best way to update this script would be greatly appreciated. Thanks!
-
Can you please tell me how to change this preg_match, which is trying to stop the html anchors. I think this has been working up until recently. Someone is placing links within the email. I also include one of the email that got through. Is it because the <a is on one line and the html is on the next line? my code: // check for html links and not permitted if (preg_match('/<a\s/', $scrubbed['comments'])) { echo '<p style="font-weight: bold; color: #C00">Not Permitted.</p>'; } else { // no html anchor the email: <p>hi!<br>for a while I wasnt feeling like myself this caught my eye right away its crazy how the tables have turned check out what I mean<br><a href="http://ansel.pl/GaryBailey15.html">http://ansel.pl/GaryBailey15.html</a><br>see you later</p>