Pull specific data from url string via php??

by gyar29
3 replies
I have a form that I'm using to post to a php script that populates a MySql database. One of the form fields is a URL. I'd like to pull specific information from the URL string and use that info as a unique id in the database.

Below you'll find an example of what I'm wanting to accomplish. The info I'd like to pull from the URL is in red font. It will be used as the unique product ID in the database.

An example URL:
Code:
h**p://somesite.com/productcategory/111111-a-specific-name-for-the-product
Of course it may be better to just generate a unique Product ID when populating the database. However, the numbers in the URL string are already unique. They are already specific to that product. They are generated by a separate script when the URL is generated so changing them is not an option. And keeping one set of product ID numbers for each product just seems to make sense.

Of course, if those of you with more experience than I are of the opinion that I'm trying to re-invent the wheel here, then I'll surrender to your wisdom and just generate another product ID when populating the database.

Thanks for the help,
Gene
#data #php #pull #specific #string #url
  • Profile picture of the author maxleadford
    Gene,

    As long as the URL format doesn't change, this will do it for you...

    PHP Code:
    <?php
    $product_id 
    $reg_results null;
    $incoming_url 'h**p://somesite.com/productcategory/12345-a-specific-name-for-the-product';
    $regexp '/([0-9]+)-';

    if (
    eregi($regexp$incoming_url$reg_results))
    {
        if (
    is_array($reg_results) && sizeof($reg_results) > 1)
        { 
    $product_id = (int) $reg_results[1]; }
    }

    if (
    $product_id 0)
    {
        
    # do your stuff here, using $product_id
    }
    ?>
    The relevant part is really the eregi() call and then the setting of $product_id from the resulting eregi()'s $reg_results array. It's wrapped in conditions for security & to keep it from throwing errors on strange results.

    To growth and success,

    Max Leadford
    {{ DiscussionBoard.errors[996411].message }}
  • Profile picture of the author gyar29
    Max,

    Excellent! Thanks for the time you spent on this.

    Take Care,
    Gene
    {{ DiscussionBoard.errors[997680].message }}
  • Profile picture of the author maxleadford
    Gene,

    You're welcome. Good luck on your project!


    To growth and success,

    Max Leadford
    {{ DiscussionBoard.errors[1004520].message }}

Trending Topics