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
https://forums.phpfreaks.com/topic/58114-find-url-in-code/
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
https://forums.phpfreaks.com/topic/58114-find-url-in-code/#findComment-288197
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
https://forums.phpfreaks.com/topic/58114-find-url-in-code/#findComment-288212
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
https://forums.phpfreaks.com/topic/58114-find-url-in-code/#findComment-288221
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.