Extract the 'image' attribute from this chunk of text via regex [closed] - 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 can find many examples of finding URL's withing HTML, however, I need to be more specific and extract the URL for the image in the following piece of text:
I will only need the first match.
I am using PHP.
Expected value result: http://cdn.somewebsite.com/path/123.jpg
var flashvars = {
'url_mode':'1',
'image':'http://cdn.somewebsite.com/path/123.jpg',
'bufferlength':'3',
'id': 'player',
'autostart': 'true'
...
};

Using preg_match with positive lookbehind assertion:
$data = <<< EOF
var flashvars = {
'url_mode':'1',
'image':'http://cdn.somewebsite.com/path/123.jpg',
'bufferlength':'3',
'id': 'player',
'autostart': 'true'
...
};
EOF;
preg_match("/(?<='image':')[^']+/", $data, $match);
echo($match[0]);

Hi this should work for you.
preg_match(/http.*?\.jpg/)

Related

convert short youtube url to full url [closed]

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);
}

Remove specific match from a string [closed]

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

php array to javascript array format [closed]

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
php array:
Array([0] => http://XXX/xgsyw/content/Uploads/Img/1111.jpg[1] => http://XXX/xgsyw/content/Uploads/Img/222.jpg)
to javascript "images"
$('#top').bgStretcher({
images: ['images/sample-1.jpg', 'images/sample-2.jpg', 'images/sample-3.jpg', 'images/sample-4.jpg', 'images/sample-5.jpg', 'images/sample-6.jpg'],
slideDirection: 'N',
slideShowSpeed: 1000,
transitionEffect: 'fade',
sequenceMode: 'normal',
});
Encode it with the JSON library. Send it back to the Javascript, decode it, and append new img child nodes to some container.
echo json_encode(arrayOfImages);
Then in your JS:
var images = JSON.decode(returnValue);
images.each(function(path) {
var img = $('<img>');
img.attr('src', returnValue);
img.appendTo('#imagediv');
});
As noted in the comments..
It is quicker to use $(document.createElement("img")); than to use the aforementioned suggestion.
You might have done some more research online, because what you need for your solution is the JavaScript Object Notation short JSON.
Use json_encode($your_array);
http://php.net/manual/de/function.json-encode.php

PHP PCRE syntax [closed]

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'm a newbie with PCRE in PHP. I'm trying to make a very basic shortcode function that could make something with a format like this one: {somealphanumericthing}
In essence I need a preg_match_all() that could find in my post these type of occurrences. I tried something like this:
$shortcode = preg_match_all('/^\b\{[a-zA-Z0-9_]+(\}\b)$/', $body, $found);
var_dump($shortcode);
if($shortcode==1) {
for($i=0;$i<count($found);$i++) {
print_r($found);
//do something nice
}
}
But unfortunately it's not working: I get int 0 to the test string {test}
A few things about the regular expression:
You don't need your line anchors since you are searching in a larger string.
There's not need to capture the closing }
Optimization, use the character class \w
Condensed:
/\b\{[a-zA-Z0-9_]+\}\b/

How to write a Loop for a Dummy? [closed]

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
this is probably an easy question, but I am not really a coder. I maintain a website for a group I belong to, and there are some php forms. I am trying to enhance a form, and I have added this php code, which works (see below). My question is, how can I put this code into a loop so that it repeats 10 times, for "01", "02", etc., "10" strings (code below is for "01" of the series)? Thanks for any help.
//Clean up the Photo Title & Maker Name to remove non-standard chars and replace spaces with underscores and generate a suggested fliename
$photo_01_clean = str_replace(" ","75357",$photo_01_name);
$photo_01_clean1 = str_replace("#","at",$photo_01_clean);
$photo_01_clean2 = preg_replace('/[^a-z0-9]/i', '', $photo_01_clean1);
$photo_01_clean3 = str_replace("75357","_",$photo_01_clean2);
$photo_01_maker_clean = str_replace(" ","75357",$photo_01_maker_name);
$photo_01_maker_clean1 = str_replace("#","at",$photo_01_maker_clean);
$photo_01_maker_clean2 = preg_replace('/[^a-z0-9]/i', '', $photo_01_maker_clean1);
$photo_01_maker_clean3 = str_replace("75357","_",$photo_01_maker_clean2);
$photo_01_filename_gen = $club_code."-01-".$photo_01_clean3."-".$photo_01_maker_clean3.".jpg";
You'd require a for loop control structure. You can find tons of resources that would teach you how to do it. example
Var i;
For (i =1; i<11; i++)
Execute Code here.

Categories