English 中文(简体)
• 如何从某一小部分获得数据,并事先获得数据?
原标题:How to get data from a certain div with preg_match?
Example: <div style="font-size:15px;">first matched here</div>

我想让PHP能够找到这一具体信息。

i 曾尝试过许多事前工作,但并不理解区域评价。

HELP?

最佳回答
<?php
$data =  aaa <div class="main">content</div> bbb ;
preg_match_all ("/<div.*?>([^`]*?)</div>/", $data, $matches);
//testing the array $matches
//$matches is an array contains all the matched div elements and its contents
//print_r($matches);
?>

Try this for matching all the div elements

问题回答

努力:

<?php
    //the string to search in
    $s = "<div style="font-size:15px;">first matched here</div>";
    //set up an empty array to facilitate the results
    $matches = array();
    //match with regex
    //pattern has to start and end with the same character (of your choice)
    //here it s #
    //start criteria is an opening div <div> that may contain some text
    //between v and >, so use .*?
    //the dot means "any character", the *? means "any number, but as few as possible"
    //then, define the part to capture. this is done with paranthesis
    //define what to capture. this would be any character, and as few as possible
    //and finally end criteria </div>
    //matches are stored in the out parameter $matches
    preg_match("#<div.*?>(.*?)</div>#", $s, $matches);
    echo "<pre>";
    print_r($matches);
    echo "</pre>";




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签