Guest MrLeN Posted February 1, 2017 Share Posted February 1, 2017 I have found this online, but all it does is print out an array. How can I get it to echo the values of the array into the page? $homepage = file_get_contents('http://www.website.com'); function getContents($str, $startDelimiter, $endDelimiter) { $contents = array(); $startDelimiterLength = strlen($startDelimiter); $endDelimiterLength = strlen($endDelimiter); $startFrom = $contentStart = $contentEnd = 0; while (false !== ($contentStart = strpos($str, $startDelimiter, $startFrom))) { $contentStart += $startDelimiterLength; $contentEnd = strpos($str, $endDelimiter, $contentStart); if (false === $contentEnd) { break; } $contents[] = substr($str, $contentStart, $contentEnd - $contentStart); $startFrom = $contentEnd + $endDelimiterLength; } return $contents; } $sample = $homepage; print getContents($sample, 'href="', '"'); Quote Link to comment Share on other sites More sharing options...
Barand Posted February 1, 2017 Share Posted February 1, 2017 use a foreach() loop or print_r() Quote Link to comment Share on other sites More sharing options...
ginerjm Posted February 1, 2017 Share Posted February 1, 2017 And how does your algorithm handle web pages that use a reversed arrangement of quotes? ie, href='domain.com/page.php' Do you call the function twice? Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted February 1, 2017 Share Posted February 1, 2017 I have found this online [...] And there's your problem. It looks like what you actually want to do is scrape a website and look for links. That's what HTML parsers are for. Parse the page, search for a elements (I assume that's what you're interested in), get their href attribute. This is far more readable, robust and flexible than any string fiddling code you may find online. Quote Link to comment Share on other sites More sharing options...
requinix Posted February 2, 2017 Share Posted February 2, 2017 Thread pruned. Next time this happens I will hand out bans. MrLeN, the best thing really is to use something that understands HTML and have it extract links or whatever. You said you tried it and couldn't get it to work? What code do you have and how is it not working? If you don't want that and think that the earlier code is good enough, The return value from getContents will be an array so you can't simply print it. As Barand said, you can use a foreach to deal with it "properly", or print_r/var_dump to simply see what's in there (like for testing). Or something else that depends on exactly what kind of output you're trying to get. Quote Link to comment Share on other sites More sharing options...
Guest MrLeN Posted February 2, 2017 Share Posted February 2, 2017 (edited) Thanks Administrator. The code above is working fine, and it is outputting exactly what I need, but is printing an array. I would like the code to echo the php code like: some kind of while loop that I really don't know how to code properly { echo $all_the_links; } But I really don't know how to code it. I did a similar thing last year (as previously mentioned), but it is a little different. The way I code stuff in php is I search for snippets online and edit it. If I can't edit it (and believe me, I try), I go looking for help. I am not very good at php, but I do spend many hours, sometimes days trying. I have been like that for 20 years. I am not not very good at programming and I never will be; and believe me, it's not because I haven't put in thousands of hours of learning. I just suck at advanced stuff. I can write basic php, and echos and variables and can I make sessions and even cookies. I know how to make a basic while loop (snippets are available online), I can use email functions, redirects, headers etc -- all basic to intermediate stuff; but I don't know how to combine "get contents between two strings" INSIDE a while loop. I know I am on the right track with the code I found (and edited) above, but I have fallen short of being able to echo the output into the page as href links. All I know is it needs a while loop somehow. Edited February 2, 2017 by MrLeN Quote Link to comment Share on other sites More sharing options...
requinix Posted February 2, 2017 Share Posted February 2, 2017 There's many ways you code show the links so you'll need to be more specific. Do you maybe know the HTML you want to see? If the links were "https://forums.phpfreaks.com" and "http://www.google.com" then what should you end up with? Quote Link to comment Share on other sites More sharing options...
Guest MrLeN Posted February 2, 2017 Share Posted February 2, 2017 (edited) There's many ways you code show the links so you'll need to be more specific. Do you maybe know the HTML you want to see? If the links were "https://forums.phpfreaks.com" and "http://www.google.com" then what should you end up with? The links are like <a href="http://www.mywebsite.com/category/a-category/">My Category</a> I want to get everything in between '/category/' and '</a>' In other words, I want want to get all instances of 'a-category' out and list them like: <a href="/category/category-1">Category 1</a><br /> <a href="/category/category-2">Category 2</a><br /> <a href="/category/category-3">Category 3</a><br /> <a href="/category/category-4">Category 4</a> Edited February 2, 2017 by MrLeN Quote Link to comment Share on other sites More sharing options...
requinix Posted February 2, 2017 Share Posted February 2, 2017 You're not doing this with your own website, right? Where are these links coming from, exactly? The BRs are annoying. How about using smarter HTML, and perhaps a dash of CSS, to get the same effect. Are you looking for a list? <ul> <li><a href="/category/category-1">Category 1</a></li>If these things represent a list, like logically they form a thing you would call "a list", then a UL would be the most appropriate way to show them in HTML; if you don't want to see bullet points then they're easy enough to remove. But there's a larger problem: getContents() will only give you the URLs. No "Category 1" and whatnot that was used as the link text. If you need that then... well, sorry, but the DOM method is not only superior to doing string math but can also get you both the link and text at the same time with no additional work (less work, in fact). Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.