Asked  7 Months ago    Answers:  5   Viewed   42 times

I want to receive an array that contains all the h1 tag values from a text

Example, if this where the given input string:

<h1>hello</h1>
<p>random text</p>
<h1>title number two!</h1>

I need to receive an array containing this:

titles[0] = 'hello',
titles[1] = 'title number two!'

I already figured out how to get the first h1 value of the string but I need all the values of all the h1 tags in the given string.

I'm currently using this to receive the first tag:

function getTextBetweenTags($string, $tagname) 
 {
  $pattern = "/<$tagname ?.*>(.*)</$tagname>/";
  preg_match($pattern, $string, $matches);
  return $matches[1];
 }

I pass it the string I want to be parsed and as $tagname I put in "h1". I didn't write it myself though, I've been trying to edit the code to do what I want it to but nothing really works.

I was hoping someone could help me out.

Thanks in advance.

 Answers

63

you could use simplehtmldom:

function getTextBetweenTags($string, $tagname) {
    // Create DOM from string
    $html = str_get_html($string);

    $titles = array();
    // Find all tags 
    foreach($html->find($tagname) as $element) {
        $titles[] = $element->plaintext;
    }
}
Wednesday, March 31, 2021
 
SpiderLinked
answered 7 Months ago
62
SELECT userID, userName, userProfession, userPic FROM tbl_users WHERE userProfession = "Teacher" ORDER BY userID DESC

is what you need.

Saturday, May 29, 2021
 
mario
answered 5 Months ago
12

Try with actual PHPDoc for your getById() where you specify dynamic type (e.g. @return static or @return $this).

That's the most common way of providing such "this class and its' children" typehint.


Another possible option is kind of the same .. but using PHP 7.1 functionality (so no PHPDoc blocks).

Try using self as return type instead of Model. I mean here:

public static function getById(string $id) : ?Model {

use this instead:

public static function getById(string $id) : ?self {

But for this one you should use PhpStorm 2017.2 EAP build as 2017.1.x has issues with that (see https://stackoverflow.com/a/44806801/783119 as an example).

Saturday, May 29, 2021
 
tiny
answered 5 Months ago
46

CURLOPT_VERBOSE should actually show the details. If you're looking for the response body content, you can also use CURLOPT_RETURNTRANSFER, curl_exec() will then return the response body.

If you need to inspect the request body, CURLOPT_VERBOSE should give that to you but I'm not totally sure.

In any case, a good network sniffer should give you all the details transparently.

Example:

$curlOptions = array(
    CURLOPT_RETURNTRANSFER => TRUE,
    CURLOPT_FOLLOWLOCATION => TRUE,
    CURLOPT_VERBOSE => TRUE,
    CURLOPT_STDERR => $verbose = fopen('php://temp', 'rw+'),
    CURLOPT_FILETIME => TRUE,
);

$url = "http://stackoverflow.com/questions/tagged/java";
$handle = curl_init($url);
curl_setopt_array($handle, $curlOptions);
$content = curl_exec($handle);
echo "Verbose information:n", !rewind($verbose), stream_get_contents($verbose), "n";
curl_close($handle);
echo $content;

Output:

Verbose information:
* About to connect() to stackoverflow.com port 80 (#0)
*   Trying 64.34.119.12...
* connected
* Connected to stackoverflow.com (64.34.119.12) port 80 (#0)
> GET /questions/tagged/java HTTP/1.1
Host: stackoverflow.com
Accept: */*

< HTTP/1.1 200 OK
< Cache-Control: private
< Content-Type: text/html; charset=utf-8
< Date: Wed, 14 Mar 2012 19:27:53 GMT
< Content-Length: 59110
< 
* Connection #0 to host stackoverflow.com left intact

<!DOCTYPE html>
<html>



<head>



    <title>Newest &#39;java&#39; Questions - Stack Overflow</title>
    <link rel="shortcut icon" href="http://cdn.sstatic.net/stackoverflow/img/favicon.ico">
    <link rel="apple-touch-icon" href="http://cdn.sstatic.net/stackoverflow/img/apple-touch-icon.png">
    <link rel="search" type="application/opensearchdescription+xml" title="Stack Overflow" href="/opensearch.xml">
...
Thursday, July 29, 2021
 
WooDzu
answered 3 Months ago
45

Based on your screenshot of Settings (Preferences on Mac) | Editor | Language Injections.

Please delete 3rd language injection rule from the bottom (the one for "div" -- that has "IDE" in Scope column).

That rule injects HTML into div tag which tells IDE to treat all other code (even PHP) inside such tag as HTML/plain text.

Thursday, August 5, 2021
 
Nil
answered 3 Months ago
Nil
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :