February 27, 2014

Regular Expressions

Regular Expressions Regular Expressions

January 31, 2014

PHP Regular Expressions to Extract Images from HTML Content

PHP Regular Expressions to Extract Images from HTML Content //preg_match_all(‘/<img[^>]+>/i’, $description, $img); //preg_match_all(‘/src=”([^”]*)”/’, $description, $img); preg_match_all(‘@<img.*src=”([^”]*)”[^>/]*/?>@Ui’, $description, $img);

August 15, 2012

JavaScript Replace All Function

JavaScript Replace All Function function replaceSubstring(inSource, inToReplace, inReplaceWith) { return inSource.replace(new RegExp(inToReplace, ‘g’),inReplaceWith); }

June 20, 2012

javascript regular expression for alphabets and space

js regular expression for alphabets and space Note: A useful javascript function which i mostly use to manage names and related fields validation. function checkName(field,title) { var val = $(“#”+field).val(); var re = /^([a-zA-Z _-]+)$/; if(val.length<1) { $(‘#error_’+field).text(title+’ is required.’); $(‘#error_’+field).addClass(‘error-msg’); return false; } else if(!re.test(val)) { $(‘#error_’+field).text(‘Please enter a valid name.’); $(‘#error_’+field).addClass(‘error-msg’); return false; […]

June 4, 2012

preg_replace to replace specific tag in string

preg_replace to replace specific tag in string if ( ! function_exists(‘clean_this’)) { function clean_this($tag_name,$html) { return preg_replace(“/(<“.$tag_name.”>.+?)+(<\/”.$tag_name.”>)/i”, ”, $html); } } Keep hyperlink text and remove href $cart_item = ‘<a>’.preg_replace(“/\<a(.*)\>(.*)\<\/a\>/iU”, “$2”, $cart_item).'</a>’;

August 4, 2011

preg_match, preg_match_all, preg_replace

To fetch all images from html content: preg_match_all(‘/]+>/i’,$content, $images_src); & $imgsrc_regex = ‘#<\s*img [^\>]*src\s*=\s*([“\’])(.*?)\1#im’; Function to get attributes of img tag: function img_plain($html,$tag=’src’) { if (stripos($html, ‘<img’) !== false) { $imgsrc_regex = ‘#<\s*img [^\>]*’.$tag.’\s*=\s*([“\’])(.*?)\1#im’; preg_match($imgsrc_regex, $html, $matches); unset($imgsrc_regex); unset($html); if (is_array($matches) && !empty($matches)) { $img = $matches[2]; return $img; } else { return false; } […]