Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6741153
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:41:27+00:00 2026-05-26T11:41:27+00:00

I often need to isolate a certain part of a string, and am not

  • 0

I often need to isolate a certain part of a string, and am not good at regular expressions. Yes, I should get better at it, but for the meantime, I’d like to know what you feel is the best way to do the following.

I’ve got some random text, often tags, from which I need to isolate a fragment. Something like this:

<title>Ask a Question - 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">
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script>
<script type="text/javascript" src="http://cdn.sstatic.net/js/stub.js?v=a1714a379225"></script>
<link rel="stylesheet" type="text/css" href="http://cdn.sstatic.net/stackoverflow/all.css?v=bd607061c911">

From this, I need to get the URL of the “apple-touch-icon”. I would do this:

<?php
// $text contains the previous blurb

// This returns an array, with the second item starting with the necessary text
$explode = explode('apple-touch-icon" href=",$text);

// This returns the position of the `"`, which is what marks the end of the string
$end_of_string = strpos('"',$explode[1]);

// Now I get only this particular part of the string
$return = substr($explode,0,$end_of_string);
?>

Voila, it works! However, I am wondering if there is an easier/better way that does not involve regular expressions.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T11:41:28+00:00Added an answer on May 26, 2026 at 11:41 am

    If you are parsing HTML, you should use something that parses HTML and not regex or string searching…that said, given your sample data and your desire to avoid regexs, you could just use strpos…I would prefer this over explode because it creates a pattern of usage that will convey your intent better.

    int strpos ( string $haystack , mixed $needle [, int $offset = 0 ] )

    $index = strpos($text, "apple-touch-icon");
    $index = strpos($text, "href=", $index);
     ...
    $return = substr($text, $index, $end-of-string);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In php, I often need to map a variable using an array ... but
Here at work, we often need to find a string from the list of
I often need to match a tuple of values that should have the same
In my web application I often need to serialize objects as JSON. Not all
I have a QThread which runs, but will often need to be killed, then
I often need to create single events that are not needed after they have
We occasionally need to notify users about warnings or problems. But often times, especially
I often need to run reduce (also called foldl / foldr, depending on your
I often need to design a dialog in Delphi/C++Builder that allows various properties of
I often need to display information based on or influenced by a user's actual

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.