How to pass only part of input value to variable and url?

So on the site i am building i need to extract only part of value from input form field and pass that to variable but i am a bit confused how to do that.

Well i made a code where

mysite.com/?getid=620

Should return some data from json file with that id.

And this works.

But!

Now the problem is that i need to get that id from url address posted in input field.

The form with input field looks like this

        <form name="appids" method="get" action="?">
            <div class="form-group">
                <label class="control-label">Insert url from Steam</label>
                <div class="input-group">
                <input class="form-control" type="text"  id="appids" name="appids" value="" placeholder="http://store.steampowered.com/app/620/" >
                <span class="input-group-btn">
                    <button class="btn btn-default" type="submit">Calculate</button>
                </span>
                </div>
            </div>
        </form>

As you can see it's bootstrap and there is already placeholder explaining user what he need to post in input field.

So the visitor just copy and paste url and i need to extract only number from that and pass it into variable or site url.

The user post this

http://store.steampowered.com/app/620/

And i need only this

620

What are the ways to do something like this ? Javascript or php code?

EDIT:

There sometime can be some type of code after the number in url, so sometime url can look like this

http://store.steampowered.com/app/241280/?snr=1_6_4__421

So i have to strip that part too ?snr=1_6_4__421

And i would try to avoid javascript because if user visit with certain mobile javascript might break sometime.

Answers


In JavaScript, you can extract app id with jQuery as user types in the field:

$('#appids').keyup(function(){
    var match = $.trim(this.value).match(/\/app\/(\d+)\/?$/);
    if (match) {
        console.log(match[1]);
    }
});

Or, preg_match can help you in PHP:

if (preg_match('@/app/(\d+)/?$@', $_GET['appids'], $match)) {
    echo 'App ID = '.$match[1];
}

Why are you not using session variable for storing a variable valus? try this`

    <?php
session_start();
// store session data
$_SESSION['views']=1;
?>

<html>
<body>

<?php
//retrieve session data
echo "Pageviews=". $_SESSION['views'];
?>

</body>
</html>

Complete lack of validation and trailing slash notwithstanding:

Javascript variant:

var url = "http://store.steampowered.com/app/620/".split("/"),
    num = url[url.length - 2];

PHP variant:

$url = split("/", "http://store.steampowered.com/app/620/");
$num = $url[count($url) - 2];

In Javascript:
var num = "http://store.steampowered.com/app/620/".replace(/.+?(\d+)(\/)?$/,"$1");
In PHP:

I don't know PHP but you can use the regex /.+?(\d+)(\/)?$/ and replace it with $1


A normal submission would do just fine: Demo

<?php
// try in your form
// http://store.steampowered.com/app/620/
// http://store.steampowered.com/app/241280/?snr=1_6_4__421

if(isset($_GET['submit'])) {
    $url = $_GET['appids'];
    $url = strtok($url, '?');
    $path = parse_url($url, PHP_URL_PATH);
    $pieces = array_filter(explode('/', $path));
    $id = end($pieces);
    echo $id;
}

?>

<form method="GET" name="appids">
    <div class="form-group">
        <label class="control-label">Insert url from Steam</label>
        <div class="input-group">
        <input class="form-control" type="text" id="appids" name="appids" value="" placeholder="http://store.steampowered.com/app/620/" >
        <span class="input-group-btn">
            <button class="btn btn-default" type="submit" name="submit">Calculate</button>
        </span>
        </div>
    </div>
</form>

Need Your Help

How to change old revision and merge into tip?

version-control mercurial

As said in the title, here is a graph to demonstrate what I want to achieve:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.