Jump to content

find URL in code


podja

Recommended Posts

Hey,

 

I am working on a script and I need to find something from some code.

 

E.g.

 

This code is inputted:

<object width="425" height="350"><param name="movie" value="
name="wmode" value="transparent"></param><embed src="
type="application/x-shockwave-flash" wmode="transparent" width="425" height="350"></embed></object>

 

I want to locate http://www.youtube.com/v/ then find all the characters after that.

 

So in this case it would return Zi_760pnGtg

 

If you need it explaining more, just say so!

 

Thanks.

Link to comment
Share on other sites

Since you know for sure that the first part is going to be youtube.com/v/

 

you can use of course regex but you can also use the faster alternative provided below...

 

<?php
$text = //the text you want to search...
$matches = array(); // will hold the matched random chars

$i = strpos($text,'youtube.com/v/');
$length = strlen('youtube.com/v/');
while ( $i !== FALSE)
{
        $l = strpos($text,'"',$i);
        if ($l !== FALSE)
        {
               $matches[] = substr($text,$i+$length,$l-$i-$length);
        }
        $i = strpos($text,'youtube.com/v/',$l);
}

?>

Link to comment
Share on other sites

That worked kathas, its just it ends up with a slash on the end.

 

And ideas how to get rid of that?

 

My code is:

 

<?php

if(isset($_POST['submit'])) {
	$input = $_POST['input'];

	$matches = array(); // will hold the matched random chars

	$i = strpos($input,'youtube.com/v/');
	$length = strlen('youtube.com/v/');

	while ( $i !== FALSE) {
		$l = strpos($input,'"',$i);
		if ($l !== FALSE) {
			$matches[] = substr($input,$i+$length,$l-$i-$length);
		}
		 $i = strpos($input,'youtube.com/v/',$l);
	}

	echo "<br>";
	echo $matches[1];
}

?>

Link to comment
Share on other sites

well the i suppose that there is a slash after the random chars in the link provided you can get rid of that many ways i would suggest the following change...

<?php

if(isset($_POST['submit'])) {
	$input = $_POST['input'];

	$matches = array(); // will hold the matched random chars

	$i = strpos($input,'youtube.com/v/');
	$length = strlen('youtube.com/v/');

	while ( $i !== FALSE) {
		$l = strpos($input,'"',$i);
		if ($l !== FALSE) {
			$matches[] = trim(substr($input,$i+$length,$l-$i-$length),'/');
		}
		 $i = strpos($input,'youtube.com/v/',$l);
	}

	echo "<br>";
	echo $matches[1];
}

?>

 

now using the trim the $matches should only contain the random chars either if the provided text has a slash after them or if it doesn't...

 

Kathas

 

Edit: i would print_r() the $matches array to see if it really works...

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.