My first rendezvous with PHP CURL

I am new to PHP CURL.
I have encountered a situation where I am stuck with preg_match_all

I am trying to pull some data and the code which is repetitive has this structure:

/*
<div class="quoteText">
      “I'm selfish, impatient and a little insecure. I make mistakes, I am out of control and at times hard to handle. But if you can't handle me at my worst, then you sure as hell don't deserve me at my best.”
  <br>  ―
  <span class="authorOrTitle">
    Marilyn Monroe
  </span>
</div>
*/

My first question is regarding preg_match_all, but the next few questions will be directly associated with CURL.

Question:

preg_match_all(
	"!!",$result,$matches
);

I am stuck here:
"!!" → How to write such code?

Can you explain what you are trying to achieve?

preg_match_all matches all instances of the pattern (argument1), in the string passed (argument2), and puts them into an array (argument3).
To help with the pattern, you need to say what you are trying to match.
Here is an example:

$string = 'I/'m selfish, impatient and a little insecure. I make mistakes...';
$pattern = '/[i am]/';
preg_match_all($pattern , $string , $matches);

The above will match all instances in the string that say “i am”.
This isn’t a very practical example, but it’s a start.
You can match a variety of different things with preg_match functions.
I’d suggest you do some reading about regex in php.

1 Like
print_r($matches);
/*
<div class="quoteText">
      “I'm selfish, impatient and a little insecure. I make mistakes, I am out of control and at times hard to handle. But if you can't handle me at my worst, then you sure as hell don't deserve me at my best.”
  <br>  ―
  <span class="authorOrTitle">
    Marilyn Monroe
  </span>
</div>
*/

I am getting error:

Warning : A non-numeric value encountered in /home3/swissknife19/public_html/…/all/app/quotescrapper/quote-scrapper.php on line 12

Line 12:
preg_match_all("!.*?<span class="/authorOrTitle/">(.*?)</span>!",$result,$matches);

The first parameter in preg_match_all is a pattern.
You need to read how to do the pattern.
Your syntax is completely incorrect.
What are you trying to match?

1 Like

This is the pattern:

<div class="quoteText">
      “I'm selfish, impatient and a little insecure. I make mistakes, I am out of control and at times hard to handle. But if you can't handle me at my worst, then you sure as hell don't deserve me at my best.”
  <br>  ―
  <span class="authorOrTitle">
    Marilyn Monroe
  </span>
</div>

I want to quote and author name for a high number of such quotes.

how about this one:

preg_match_all("!<div class="/quoteText/">(.*?)</div><span class="/authorOrTitle/">(.*?)</span>!",$result,$matches);

Are you trying to match that entire quote?
The entire Marilyn Manroe quote, with all the tags, and everything?

1 Like

preg_match should be used a last resort only. preg_match can solve a lot of problems, but most of those problems should be solved differently.

Actually parsing the HTML works a lot better: http://htmlparsing.com/php.html

2 Likes

what does this do:

".*?"

Trying to scrap. the div and span will remain the same, but while using curl element: the quote and the author will differ each time.

rpkamp’s point remains. Don’t use preg where something else will do the job.

If there will be exactly 1 quote in your result, you could do it like this:

$quote = reset(explode("<",end(explode("quoteText\">",$inputstr))));
$author = reset(explode("<",end(explode("authorOrTitle\">",$inputstr))));

EDIT: Helps if i put my parameters in the right order…

I am interested in implementing your solution, but I could not understand it fully.

Confused. what should I do with them?
BTW I was following this tutorial.
As you notice that the code at the time of shooting that video is different now, slightly though.

You wanted the quote and the author. $quote is the quote. $author is the author. What you do with them is beyond my instruction.

What part do you not understand?

1 Like

Hi everyone,

finally I was able to get it done, and get result the way I wanted.

Here is the full working code →

<?php
$curl = curl_init();
$start_page = 1;
$end_page = 3; //upto 100 pages.
$data = array();
$counter = 0;

// Curl settings
curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($curl,CURLOPT_RETURNTRANSFER,true);
// echo $result;
$url = 'https://www.example.com/quotes/tag/love?page=1';
curl_setopt($curl,CURLOPT_URL,$url);
$result  = curl_exec($curl);
preg_match_all("!&ldquo;(.*?)&rdquo;.*?<span class=\"authorOrTitle\">(.*?)</span>!is",$result,$matches);
curl_close($curl);
print_r($matches);

/*
<div class="quoteText">
      &ldquo;I'm selfish, impatient and a little insecure. I make mistakes, I am out of control and at times hard to handle. But if you can't handle me at my worst, then you sure as hell don't deserve me at my best.&rdquo;
  <br>  &#8213;
  <span class="authorOrTitle">
    Marilyn Monroe
  </span>
</div>
*/

N.B. I have hidden the actual CURL URL because there might be some violation issue for sitepoint.com in future

Question:
HTML Patter on which preg match is defined:

/*
<div class="quoteText">
      &ldquo;I'm selfish, impatient and a little insecure. I make mistakes, I am out of control and at times hard to handle. But if you can't handle me at my worst, then you sure as hell don't deserve me at my best.&rdquo;
  <br>  &#8213;
  <span class="authorOrTitle">
    Marilyn Monroe
  </span>
</div>
*/

preg_match_all("!&ldquo;(.*?)&rdquo;.*?<span class=\"authorOrTitle\">(.*?)</span>!is",$result,$matches);

In this →
print_r($matches);

The array is printed here →

Apart from the full array it is also printing separate array for quotes and authors:

   [1] => Array
        (
            [0] => I'm selfish, impatient and a little insecure. I make mistakes, I am out of control and at times hard to handle. But if you can't handle me at my worst, then you sure as hell don't deserve me at my best.
            [1] => You've gotta dance like there's nobody watching,<br />Love like you'll never be hurt,<br />Sing like there's nobody listening,<br />And live like it's heaven on earth.
            [2] => You know you're in love when you can't fall asleep because reality is finally better than your dreams.
            [3] => A friend is someone who knows all about you and still loves you.
            [4] => Darkness cannot drive out darkness: only light can do that. Hate cannot drive out hate: only love can do that.
            [5] => We accept the love we think we deserve.
            [6] => It is better to be hated for what you are than to be loved for what you are not.
            [7] => Only once in your life, I truly believe, you find someone who can completely turn your world around. You tell them things that you’ve never shared with another soul and they absorb everything you say and actually want to hear more. You share hopes for the future, dreams that will never come true, goals that were never achieved and the many disappointments life has thrown at you. When something wonderful happens, you can’t wait to tell them about it, knowing they will share in your excitement. They are not embarrassed to cry with you when you are hurting or laugh with you when you make a fool of yourself. Never do they hurt your feelings or make you feel like you are not good enough, but rather they build you up and show you the things about yourself that make you special and even beautiful. There is never any pressure, jealousy or competition but only a quiet calmness when they are around. You can be yourself and not worry about what they will think of you because they love you for who you are. The things that seem insignificant to most people such as a note, song or walk become invaluable treasures kept safe in your heart to cherish forever. Memories of your childhood come back and are so clear and vivid it’s like being young again. Colours seem brighter and more brilliant. Laughter seems part of daily life where before it was infrequent or didn’t exist at all. A phone call or two during the day helps to get you through a long day’s work and always brings a smile to your face. In their presence, there’s no need for continuous conversation, but you find you’re quite content in just having them nearby. Things that never interested you before become fascinating because you know they are important to this person who is so special to you. You think of this person on every occasion and in everything you do. Simple things bring them to mind like a pale blue sky, gentle wind or even a storm cloud on the horizon. You open your heart knowing that there’s a chance it may be broken one day and in opening your heart, you experience a love and joy that you never dreamed possible. You find that being vulnerable is the only way to allow your heart to feel true pleasure that’s so real it scares you. You find strength in knowing you have a true friend and possibly a soul mate who will remain loyal to the end. Life seems completely different, exciting and worthwhile. Your only hope and security is in knowing that they are a part of your life.
            [8] => As he read, I fell in love the way you fall asleep: slowly, and then all at once.
            [9] => The opposite of love is not hate, it's indifference. The opposite of art is not ugliness, it's indifference. The opposite of faith is not heresy, it's indifference. And the opposite of life is not death, it's indifference.
            [10] => It is not a lack of love, but a lack of friendship that makes unhappy marriages.
            [11] => I love you without knowing how, or when, or from where. I love you simply, without problems or pride: I love you in this way because I do not know any other way of loving but this, in which there is no I or you, so intimate that your hand upon my chest is my hand, so intimate that when I fall asleep your eyes close.
            [12] => Love all, trust a few, do wrong to none.
            [13] => Have you ever been in love? Horrible isn't it? It makes you so vulnerable. It opens your chest and it opens up your heart and it means that someone can get inside you and mess you up.
            [14] => Being deeply loved by someone gives you strength, while loving someone deeply gives you courage.
            [15] => This life is what you make it. No matter what, you're going to mess up sometimes, it's a universal truth. But the good part is you get to decide how you're going to mess it up. Girls will be your friends - they'll act like it anyway. But just remember, some come, some go. The ones that stay with you through everything - they're your true best friends. Don't let go of them. Also remember, sisters make the best friends in the world. As for lovers, well, they'll come and go too. And baby, I hate to say it, most of them - actually pretty much all of them are going to break your heart, but you can't give up because if you give up, you'll never find your soulmate. You'll never find that half who makes you whole and that goes for everything. Just because you fail once, doesn't mean you're gonna fail at everything. Keep trying, hold on, and always, always, always believe in yourself, because if you don't, then who will, sweetie? So keep your head high, keep your chin up, and most importantly, keep smiling, because life's a beautiful thing and there's so much to smile about.
            [16] => There is never a time or place for true love. It happens accidentally, in a heartbeat, in a single flashing, throbbing moment.
            [17] => Love is that condition in which the happiness of another person is essential to your own.
            [18] => You may not be her first, her last, or her only. She loved before she may love again. But if she loves you now, what else matters? She's not perfect—you aren't either, and the two of you may never be perfect together but if she can make you laugh, cause you to think twice, and admit to being human and making mistakes, hold onto her and give her the most you can. She may not be thinking about you every second of the day, but she will give you a part of her that she knows you can break—her heart. So don't hurt her, don't change her, don't analyze and don't expect more than she can give. Smile when she makes you happy, let her know when she makes you mad, and miss her when she's not there.
            [19] => You love me. Real or not real?"<br />I tell him, "Real.
            [20] => I'm in love with you," he said quietly.<br /><br />"Augustus," I said.<br /><br />"I am," he said. He was staring at me, and I could see the corners of his eyes crinkling. "I'm in love with you, and I'm not in the business of denying myself the simple pleasure of saying true things. I'm in love with you, and I know that love is just a shout into the void, and that oblivion is inevitable, and that we're all doomed and that there will come a day when all our labor has been returned to dust, and I know the sun will swallow the only earth we'll ever have, and I am in love with you.
            [21] => I am nothing special, of this I am sure. I am a common man with common thoughts and I've led a common life. There are no monuments dedicated to me and my name will soon be forgotten, but I've loved another with all my heart and soul, and to me, this has always been enough..
            [22] => Love looks not with the eyes, but with the mind,<br />And therefore is winged Cupid painted blind.
            [23] => Love is like the wind, you can't see it but you can feel it.
            [24] => People think a soul mate is your perfect fit, and that's what everyone wants. But a true soul mate is a mirror, the person who shows you everything that is holding you back, the person who brings you to your own attention so you can change your life. <br /><br />A true soul mate is probably the most important person you'll ever meet, because they tear down your walls and smack you awake. But to live with a soul mate forever? Nah. Too painful. Soul mates, they come into your life just to reveal another layer of yourself to you, and then leave. <br /><br />A soul mates purpose is to shake you up, tear apart your ego a little bit, show you your obstacles and addictions, break your heart open so new light can get in, make you so desperate and out of control that you have to transform your life, then introduce you to your spiritual master...
            [25] => You don't love someone because they're perfect, you love them in spite of the fact that they're not.
            [26] => If you can make a woman laugh, you can make her do anything.
            [27] => We’re all a little weird. And life is a little weird. And when we find someone whose weirdness is compatible with ours, we join up with them and fall into mutually satisfying weirdness—and call it love—true love.
            [28] => There is nothing I would not do for those who are really my friends. I have no notion of loving people by halves, it is not my nature.
            [29] => Love never dies a natural death. It dies because we don't know how to replenish its source. It dies of blindness and errors and betrayals. It dies of illness and wounds; it dies of weariness, of witherings, of tarnishings.
        )

    [2] => Array
        (
            [0] => 
    Marilyn Monroe
  
            [1] => 
    William W. Purkey
  
            [2] => 
    Dr. Seuss
  
            [3] => 
    Elbert Hubbard
  
            [4] => 
    Martin Luther King Jr.,
  
            [5] => 
    Stephen Chbosky,
  
            [6] => 
    Andre Gide,
  
            [7] => 
    Bob Marley
  
            [8] => 
    John Green,
  
            [9] => 
    Elie Wiesel
  
            [10] => 
    Friedrich Nietzsche
  
            [11] => 
    Pablo Neruda,
  
            [12] => 
    William Shakespeare,
  
            [13] => 
    Neil Gaiman,
  
            [14] => 
    Lao Tzu
  
            [15] => 
    Marilyn Monroe
  
            [16] => 
    Sarah Dessen,
  
            [17] => 
    Robert A. Heinlein,
  
            [18] => 
    Bob Marley
  
            [19] => 
    Suzanne Collins,
  
            [20] => 
    John Green,
  
            [21] => 
    Nicholas Sparks,
  
            [22] => 
    William Shakespeare,
  
            [23] => 
    Nicholas Sparks,
  
            [24] => 
    Elizabeth Gilbert,
  
            [25] => 
    Jodi Picoult,
  
            [26] => 
    Marilyn Monroe
  
            [27] => 
    Robert Fulghum,
  
            [28] => 
    Jane Austen,
  
            [29] => 
    Anais Nin
  
        )

)

It is a default feature of preg_all_match or because the string is from curl that is why it is happening?

https://www.php.net/manual/en/function.preg-match-all.php

Did you read the manual to understand the function you’re using?

2 Likes

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.