Example of a PHP regular to extract/replace/Add ALT tags to a picture

Source: Internet
Author: User

Example

$arcrow [' title ']= ' article titles ';
$arcrow [' content ']= ' article body ';

$pattern = "/(]*?alt=[^<>]*?>)) (.*?>)/is "; Add an alt label to a picture
$replacement = "\$1 alt= '". Strip_tags ($arcrow [' title ']). "' \$2";
$arcrow [' content '] = preg_replace ($pattern, $replacement, $arcrow [' content ']);

PHP instance:

Use regular expressions to replace substrings (Alt tags) of strings (picture labels) with the following code:
<?php
$str =<<<start
<a href= "http://www.111cn.net" target= "_blank" alt= "1231dsfdsfds" >aaa</a>
Start
$pattern = '/alt= ' \d+[a-z]+\ '/I ';
$replace = ';
$result =preg_replace ($pattern, $replace, $STR);
echo $result;
?>
Output results <a href= "http://www.111cn.net" target= "_blank" >aaa</a>
Remove the contents of ALT and Alt inside.

Example

PHP regular fetch img tag alt src width Heigh property

<?php
/* Regular take picture of any attribute in the IMG tag * *
$word = "<p height=" align= "Cenetr" >111 22</p> <div style=" float:left; > Chinese </div> ';
Take the whole picture code
Preg_match ('//is ', $word, $matches);
echo $matches [0];//results:

$word = ' <p height= ' align= "Cenetr" >111 22</p> <div style=" float:left; > Chinese </div> ';
Take width
Preg_match ('//i ', $word, $matches);
echo $matches [1];
Take height
Preg_match ('//i ', $word, $matches);
echo $matches [1];
Take SRC
Preg_match ('//i ', $word, $matches);
echo $matches [1];
/* Regular replacement Remove or change any attribute in the image img Tag ***************************************************************/
$str = ' <p height= "align=" Cenetr ">111 22</p> <div style= "float:left;"  > Chinese </div><p height= align= "Cenetr" >31313 224344</p> <div style= "float:left;" >1212121</div> ';

Change the src attribute (where the original src= "/upload/images/bbb.jpg" is changed to Src= "/upload/_thumbs/images/bbb.jpg")
Print Preg_replace ('/()/I ', "${1}_thumbs/images/${3}", $STR);

/* Change src attribute,
Here the original src= "/upload/images/bbb.jpg" changed to Src= "/upload/_thumbs/images/bbb.jpg", and discard width and height
(for example, you want to display thumbnails in the foreground, but the database tutorial stores the path of the original image.) Why do you want to abandon the width of the high? You thumbnails Ah! or the original image of the width of the height, how will???)
*/
Print Preg_replace ('/(/i ', "${1} ${2}_thumbs/images/${3}>", $STR);

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.