Preg_match - strange output?

software development

#1

I’m trying to extract pieces from a string, but the output I get on DreamHost is different than expected. Any ideas?

String:

1st part (extracting URL):

returns: http://www.youtube.com/v/Dzr32OqGlOA&hl=en
Why the backslash???

2nd part (extracting width value):

no output.

there should be plus signs after each [] group but the forum won’t show them.

Please help! :slight_smile:

Just to clarify, these expressions WORK perfectly on another server.


#2

Why not try the approach of parsing the attributes out, then the values.

[code]<?php

preg_match_all(’/\s([A-Za-z][0-9A-Za-z_:.-])="([^"])"/i’, $html, $attributes, PREG_SET_ORDER);

echo “Attributes:

”;
print_r($attributes);
echo “
\n”;

foreach ($attributes as $attribute) {
$name = $attribute[1];
$value = $attribute[2];
if (strcasecmp(‘SRC’, $name) == 0) {
$stringURL = $value;
}
if (strcasecmp(‘WIDTH’, $name) == 0) {
$stringWidth = $value;
}
}

echo “URL: $stringURL
Width: $stringWidth\n”;

php?>[/code] :cool: openvein.org -//- One-time [color=#6600CC]$50.00 discount[/color] on [color=#0000CC]DreamHost[/color] plans: Use ATROPOS7


#3

Wow, wish I knew about that :slight_smile:

Problem is, it works on my system perfectly, but not on Dreamhost. All I get for output is:

Attributes:
Array
(
)
URL: Array
Width: Array

It’s as if Dreamhost treats string’s differently than PHP. Is there a difference between PHP_MOD and PHP_CGI that could cause this?

if I use print_r I get

[quote]Array ( [0] => http://www.youtube.com/v/Dzr32OqGlOA&hl=en\ [1] => [2] => [3] => www.youtube.com/v/Dzr32OqGlOA&hl=en\ )
Array ( ) [/quote]

Those 's are still there! Grr.


#4

Turns out if I use mod_php it causes all kinds of issues. As soon as I used
AddHandler php5-cgi .html
instead of ForceType
in my .htaccess file
it works properly now!

Wohoo :slight_smile:

Thanks for your help, that little bit of script will come in handy later.