Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Hi all i am looking for a simple way to check if a string equals an url like this:
http://youtu.be/WWQZ046NeUA
To convert it to a proper youtube url like this:
http://www.youtube.com/watch?v=WWQZ046NeUA
If not to leave it alone, what's the simplest way to do it in php?
You can use this preg_replace call:
$u = 'http://youtu.be/WWQZ046NeUA';
$r = preg_replace('~^https?://youtu\.be/([a-z\d]+)$~i', 'http://www.youtube.com/watch?v=$1', $u);
str_replace should work wonders.
$url = ''; //url you're checking
$ytshorturl = 'youtu.be/';
$ytlongurl = 'www.youtube.com/watch?v=';
if (strpos($url,$yturl) !== false) {
$url = str_replace($ytshorturl, $ytlongurl, $url);
}
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Can anyone help me get the value of IFC/USD 0.00001031 from the webpage http://infinitecoin.com.
I need to grab the value after IFC/USD..
The way I thought to do it was to:
$file_string = file_get_contents('http://infinitecoin.com');
preg_match('/IFC/USD plus next 11 charecters', $file_string, $title);
$value = $title[1];
echo $title_out ;
But im not sure how to ask PHP to find IFC/USD then return the next 11 characters after that.
If I could accomplish this, my task would be solved..
Any help would be great.
Thank you
Jason
$output = array();
preg_match("/(?<=IFC\/USD\s)[\d\.]+/", 'IFC/USD 0.00001015', $output);
$output will look like this:
Array
(
[0] => 0.00001015
)
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want to remove a particular match of characters from a string.
For example i have strings;
topics-p10-new-model-cars
topics-p20-new-model-cars
topics-p30-new-model-cars
topics-p40-new-model-cars
Then i need the results as,
topics-new-model-cars
topics-new-model-cars
topics-new-model-cars
topics-new-model-cars
That means i want to remove p10-,p20-,etc..
.Those are the page numbers. It may be any number..
How can i do this..? Thanks in advance
Try this:
$result = preg_replace('/\-p\d+/', '', $string);
Note: I'm assuming that the string format does not change (I mean this [topics-p10-new-model-cars]). If my assumption is right.
Then you can do this
if (textBox1.Text.Contains("-p10-"))
{
//topics-p10-new-model-cars
String[] splited = textBox1.Text.Split(new char[] {'-'});
String rString = String.Format("{0}-{1}-{2}-{3}",
splited[0],splited[2],splited[3],splited[4]);
MessageBox.Show(rString);
}
//OR This method
if (textBox1.Text.Contains("-p10-"))
{
String result = textBox1.Text.Replace("p10-", "");
MessageBox.Show(result);
}
With 'preg-replace'::
For, example:
<?
echo preg_replace('/p[0-9]+\-/', '', 'topics-p10-new-model-cars');
?>
Follow this link:
http://www.php.net/manual/en/function.preg-replace.php
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a string : http://www.mywebsite/456754567/531613490.htm?menu=contact
I want to get the value between "/" and ".htm". Here : 531613490
Any idea ?
if ( preg_match('~[^/]+(?=\.htm)~', $string, $matches) ) {
echo $matches[0];
}
Here's a demo: http://codepad.viper-7.com/5kjEj6
I don't know how flexible you want your script to be, but here's my try:
It always takes the last part of the path
It won't fail for http://www.mywebsite/456754567.htm/531613490.htm?menu=contact' as Joseph Silber's solution does.
<?php
$path = parse_url('http://www.mywebsite/456754567/531613490.htm?menu=contact', PHP_URL_PATH);
$pathParts = explode('/', $path);
$fullFilename = array_pop($pathParts);
// better use something like lastIndexOf(), this won't fail for 'xxx.abc.htm'
$filename = substr($fullFilename, 0, strpos($fullFilename, '.'));
var_dump( $filename );
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have an SMF website and i'm actually trying to get some header information which includes the title of a particular thread, the url but i've been finding it difficult to get the unique link affixed to the url using PHP.
Here's the url: http://example.com/index.php?topic=6449.msg6858
I'm actually looking for a way to extract the number 6449, I've tried to use the php GET function but it doesn't work.
$parts = explode('.', $_GET['topic']);
echo $parts[0];
// PHP 5.4+
echo explode('.', $_GET['topic'])[0];
See it in action
This would work, too
echo (int) $_GET['topic'];
See it in action
You want to use a combination of substr and strpos (to find the first occurence of a period)
$number = substr($_GET['topic'], 0, strpos($_GET['topic'], '.'));
// 6449
$arr = array();
if (preg_match("/([\\d]+)([.]{1}msg[\\d]+)/", $_GET["topic"], $arr) == 1) {
echo $arr[1];
} else {
trigger_error("not found", E_USER_ERROR);
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want to trim this string after "=" and before ";"
com=8af14f8b9d5be16e807ab9e0ed1d7edb:b6668c681f9d3bd2ad4ae31a8b6c7f2859939a75;
so that it echoes 8af14f8b9d5be16e807ab9e0ed1d7edb:b6668c681f9d3bd2ad4ae31a8b6c7f2859939a75
You can use the str_replace function to replace multiple values by, in this case, nothing:
str_replace(array('com=', ';'), '', $string);
This can also work:
parse_str(trim(
'com=8af14f8b9d5be16e807ab9e0ed1d7edb:b6668c681f9d3bd2ad4ae31a8b6c7f2859939a75;'
, ';'));
echo "$com\n";
Try this:
$value="aaaacom=8af14f8b9d5be16e807ab9e0ed1d7edb:b6668c681f9d3bd2ad4ae31a8b6c7f2859939a75;";`
$pos=strpos($value,"=");
echo substr($value,$pos+1,strlen($value)-($pos+2));
You can use a regex:
$str = 'com=8af14f8b9d5be16e807ab9e0ed1d7edb:b6668c681f9d3bd2ad4ae31a8b6c7f2859939a75;';
echo preg_replace('/([a-z]+)=(.*);/', '\2', $str);
But it looks like a .ini file, then maybe you want this:
Try this:
$ini = 'com=8af14f8b9d5be16e807ab9e0ed1d7edb:b6668c681f9d3bd2ad4ae31a8b6c7f2859939a75;';
$parsed_ini = parse_ini_string($ini);
echo $parsed_ini['com'];