Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
PHP regex - modifying HTML element V2
<?php $html = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:v="urn:schemas-microsoft-com:vml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0"/> <title>Email</title> <style type="text/css"> v:* { behavior: url(#default#VML); display: inline-block; } #outlook a {padding:0;} body{width:100% !important; -webkit-text-size-adjust:100%; -ms-text-size-adjust:100%; margin:0; padding:0;background:#fff;} .ExternalClass {width:100%;} .ExternalClass, .ExternalClass p, .ExternalClass span, .ExternalClass font, .ExternalClass td, .ExternalClass div {line-height: 100%;} #backgroundTable {margin:0; padding:0; width:100% !important; line-height: 100% !important;} img {outline:none; text-decoration:none; -ms-interpolation-mode: bicubic;} a img {border:none;} .image_fix {display:block;} p {margin: 0.1em 0 1em;} h1 a:active, h2 a:active, h3 a:active, h4 a:active, h5 a:active, h6 a:active { color: red !important; } h1 a:visited, h2 a:visited, h3 a:visited, h4 a:visited, h5 a:visited, h6 a:visited { color: purple !important; } table td {border-collapse: collapse;} table { border-collapse:collapse; mso-table-lspace:0pt; mso-table-rspace:0pt; } a {color: #0088cc;} @media only screen and (max-device-width: 480px) { a[href^="tel"], a[href^="sms"] { text-decoration: none; color: black; /* or whatever your want */ pointer-events: none; cursor: default; } .mobile_link a[href^="tel"], .mobile_link a[href^="sms"] { text-decoration: default; color: orange !important; /* or whatever your want */ pointer-events: auto; cursor: default; } } @media only screen and (min-device-width: 768px) and (max-device-width: 1024px) { a[href^="tel"], a[href^="sms"] { text-decoration: none; color: blue; /* or whatever your want */ pointer-events: none; cursor: default; } .mobile_link a[href^="tel"], .mobile_link a[href^="sms"] { text-decoration: default; color: orange !important; pointer-events: auto; cursor: default; } } @media screen and (max-width: 600px){ #wrapperTable { width:auto; max-width:550px; } } </style> </head> <body> <table cellpadding="0" cellspacing="0" border="0" align="center" id="backgroundTable" style="width:100%;" width="100%" bgcolor="#ffffff"> <tr><td> <br> <table cellpadding="0" border="0" cellspacing="0" align="center" id="wrapperTable" style="width:550px;" width="550"> <tr><td bgcolor="#ffffff" style="border:0px;background:#ffffff"> <table cellpadding="0" cellspacing="0" style="width:100%;" width="100%"> <tr><td><table style="border-top:1px solid #ddd;border-bottom:1px solid #ddd;table-layout:fixed;margin:5px 0" width="100%" cellpadding="0" cellspacing="0" border="0"><tr><td style="width:364px;vertical-align:top;padding:10px 10px 10px 0"><h2 style="margin:0;font-size:18pt;font-weight:600;line-height:130%;font-family:arial,sans-serif;color:#fe6a12;word-wrap:break-word"><span style="color:#*|r0|*">*|r1|*</span></h2><h3 style="margin:8px 0;font-size:14pt;font-weight:600;line-height:130%;font-family:arial,sans-serif;color:#aaa;word-wrap:break-word">*|r2|*</h3><p style="margin:0;font-size:12pt;font-weight:400;line-height:130%;font-family:arial,sans-serif;color:#aaa;word-wrap:break-word">*|r3|*</p></td><td style="width:168px;vertical-align:top;line-height:0px;padding:10px 0"><img src="*|r4|*" width="168" height="133" alt="logo" style="display:block" /></td></tr></table></td></tr><tr><td><table width="100%" cellpadding="0" cellspacing="0"><tr><td style="padding-top:0px;padding-bottom:0px;"><table width="100%" cellpadding="0" cellspacing="0" style="width:100%;"><tr><td style="font-size:11pt;font-family:arial,sans-serif;mso-line-height-rule:exactly;line-height:150%;color:#000;background-color:#ffffff"><table width="100%"><tr><td align="center"><table cellpadding="8" style="margin:0 auto 20px;width:190px;"><tr><td style="background-color:#88ae23;text-align:center;"><img alt="" src="http://192.168.99.100/img/tick.png" style="float:left;margin:3px 5px 0 5px;" /><span style="color:#fff;font-size:11pt;font-family:arial, sans-serif;">Confirmation</span></td></tr></table></td></tr></table><p><span style="color:#696969;">Dear *|r5|* *|r6|*,<br /><br />Thank you for registering for *|r1|*. If you have any questions about the event, get in touch with us at <a href="mailto:*|r8|*">*|r8|*</a>.</span></p><p> </p><p><span><strong>*|r1|* Team</strong></span></p><p>*|r11|*</p></td></tr></table></td></tr></table></td></tr> </table> </td></tr> </table> <br></td></tr> </table> </body></html>'; $imagePattern = '/\\<img.+src\\=(?:\\"|\\\')(.+?)(?:\\"|\\\')(?:.+?)\\s+alt\\=(\\"|\\\')logo(\\"|\\\').+\\/\\>/'; $imagePattern2 = '"\\<img.+?src=(?<imageSource>(?:\\"|\\\')[^\\s]*(?:\\"|\\\')).+?alt=(?:\\"|\\\')logo(?:\\"|\\\').+?(?:\\/>|>)"'; $httpPattern = '\'^(http:\\/\\/www\\.|https:\\/\\/www\\.|http:\\/\\/|https:\\/\\/)[a-z0-9]+([\\-\\.]{1}[a-z0-9]+)*\\.[a-z]{2,5}(:[0-9]{1,5})?(\\/.*)?$\''; $srcPattern = '@src="([^"]+)"@'; // get all images // preg_match_all($imagePattern2, $html, $imagesMatch); // print_r("Images in HTML:"); // echo "\r\n"; // print_r($imagesMatch); // echo "\r\n"; // echo "\r\n"; // $shiftedImage = array_shift($imagesMatch); // print_r("Shifted Image:"); // echo "\r\n"; // print_r($shiftedImage); // echo "\r\n"; // print_r("Image is not Empty: ". !empty("https://test.com/me")); // echo "\r\n"; // echo "\r\n"; // get image with alt="logo" from html preg_match_all($imagePattern2, $html, $imageMatches); print_r("Matches with alt='logo' from HTML:"); echo "\r\n"; print_r($imageMatches); echo "\r\n"; echo "\r\n"; // flatten the array $flattened = flatten_array($imageMatches); print_r("Flattened Array:"); echo "\r\n"; print_r($flattened); echo "\r\n"; // get image source $imgSrc = array_shift(array_slice($flattened, 1, 1)); // $imgSrc = array_shift($flattened); $imgSrcArray[] = $imgSrc; print_r("Image Source: $imgSrc"); echo "\r\n"; print_r($imgSrcArray); echo "\r\n"; echo "\r\n"; $isValidHTTPImageSource = preg_match($httpPattern, $imgSrc); // not a valid source print_r("is this a valid HTTP source?: $isValidHTTPImageSource"); echo "\r\n"; echo "\r\n"; if(!$isValidHTTPImageSource) { $newImageElement = preg_replace($imagePattern2, '[REPLACED IMG]', $html); //print_r($newImageElement); echo "\r\n"; echo "\r\n"; } $imgUrl = 'https://test.com/me/replace'; $newImgSrc = preg_replace('"\\*\\|r[0-9]\\|\\*"', $imgUrl, $imgSrc); print_r("New Image Source: $newImgSrc"); echo "\r\n"; echo "\r\n"; $isValidHTTPsource = filter_var($imgSrc, FILTER_VALIDATE_URL); if($isValidHTTPsource) { print_r("This is a VALID http Source: "); print_r($isValidHTTPsource); echo "\r\n"; echo "\r\n"; } $templateVariables = [ [ "name" => "r3", "content" => "test" ], [ "name" => "r4", "content" => "https://dcassetcdn.com/design_img/2505971/552274/552274_13470292_2505971_9c972622_image.png" ], ]; print_r('----- Plucking out Template Variable image source'); echo "\r\n"; echo "\r\n"; $templateVariable = array_filter($templateVariables, function($variable) use(&$imgSrc){ $name = $variable['name']; return preg_match("'$name'", $imgSrc); }); $templateVariable = flatten_array($templateVariable); $pluckedImageSource = array_pop($templateVariable); print_r("Plucked image source: $pluckedImageSource"); echo "\r\n"; echo "\r\n"; print_r("Is plucked image source valid? : "); print_r(filter_var($pluckedImageSource, FILTER_VALIDATE_URL)); echo "\r\n"; echo "\r\n"; function flatten_array($array) { array_walk_recursive($array, function($item) use (&$new_array){ $new_array[]=$item; }); return $new_array; }
run
|
edit
|
history
|
help
0
Php3
سلام من مینام
Lock and unlock
Add_adress
AAA
Problem: rstring
Php4
First.php
website wizard
q