Jump to content

Recommended Posts

Hi there.

 

I'll be honest.  I'm totally new to regular expressions and therefore slightly without a clue.  I'm attempting to resize default embedded video dimensions so that the video looks better with the layout of my page.

 

Can anyone explain what I'm doing wrong, please?

 

<?php
  //resize embedded video
  $find = preg_match("/<iframe(.*)/iframe>/", $eventDescLong, $video);

  if ($find) {

  $video = preg_replace('/(width)=("[^"]*")/i', 'width="580"', $video);
  $video = preg_replace('/(height)=("[^"]*")/i', 'height="387"', $video);

  }

 

Thanks,

 

Andy

Link to comment
https://forums.phpfreaks.com/topic/235976-trying-to-automate-video-resizing/
Share on other sites

Sure. 

 

$eventDescLong, is basically the detailed description the an act playing at a live music venue.  I'd like to give the venue the opportunity to incorporate embedded video as part of this.  It's an underground venue, so we have a lot of acts no-one has heard of playing!  The video is basically youtube footage of the acts from other venues, or music videos they have made.  This actually works fine atm.  It just look a bit ugly as it's out of sync with my page layout which is why I'd like to change the embedded footage size from its default values.

 

However, I'll be honest.  I've never really touched regular expressions before.  So I don't have a clue!

 

Here's the script that posts the form information to the database.  It all seems to work, apart from the video dimension resizing script originally posted.

 

<?php

  //combines variables $eventYear, $eventMonth & eventDay to variable '$eventDate'.

  $eventYear = $_POST['eventYear'];

  $eventMonth = $_POST['eventMonth'];

  $eventDay = $_POST['eventDay'];

  $eventDate = "$eventYear-$eventMonth-$eventDay";

  //assigns contents of 'listing_artist' to variable '$listing_artist'.

  $eventName = $_POST['eventName'];

  //assigns contents of 'listing_desc' to variable '$listing desc'.

  $eventDescLong = $_POST['eventDescLong'];

  //assigns contents of 'eventEntryFee' to variable '$eventEntryFee'.

  $eventEntryFee = $_POST['eventEntryFee'];

// Include script to search for video content and resize it.

  include ('resize_video.php');

  // Include database connection code.

  include ('dbConnect.php');

  $queryTable = "INSERT INTO eventsTable (

    eventDate, eventName, eventDescLong, eventEntryFee

    )

    VALUES (

    '$eventDate', '$eventName', '$eventDescLong', '$eventEntryFee'

    )";

  $result = mysql_query($queryTable, $connection)

    or die (mysql_error());

  mysql_close ($connection);

  include ('redirectToEdit.php');

?>

Ah!

 

That value is just a youtube iframe.  In this instance:

 

<iframe width="425" height="349" src="http://www.youtube.com/embed/oSxWqfayU9E" frameborder="0" allowfullscreen></iframe>

 

I'd like the code to be able to recognise any video iframe.

 

$eventDescLong = '<iframe width="425" height="349" src="http://www.youtube.com/embed/oSxWqfayU9E" frameborder="0" allowfullscreen></iframe>';
$video = preg_replace('~width\s?=\s?["\'][^"\']+["\']~i', 'width="580"', $eventDescLong);
$video = preg_replace('~height\s?=\s?["\'][^"\']+["\']~i', 'height="387"', $video);

That'd do it in this instance. 

 

However, some of the long event descriptions also contain descriptive text, as well as a video.  For example:

 

'Conquering Animal Sound provided one of our most memorable shows in late 2010. Wilfully angular Scottish boy/girl duo with their excellent L.P, affection for Toy Keyboards, and their other worldly sounds. This is highly recommended!

 

<iframe width="425" height="349" src="http://www.youtube.com/embed/4D_iv0Dyg-k" frameborder="0" allowfullscreen=""></iframe>'

 

How would I get the script to scan the entry, and locate the appropriate part to replace?

Well, if you know your $eventDescLong variable is only ever gonna hold that one iframe, or more accurately, one place where you have a width=".." and height=".." then you can use the same patterns, though just work directly with $eventDescLong instead of using $video:

 

$eventDescLong = preg_replace('~width\s?=\s?["\'][^"\']+["\']~i', 'width="580"', $eventDescLong);
$eventDescLong = preg_replace('~height\s?=\s?["\'][^"\']+["\']~i', 'height="387"', $eventDescLong);

 

But if you know you will have other things in $eventDescLong (like images) that will have a width=".." or height=".." then you can do this:

 

$eventDescLong = preg_replace('~(<iframe[^>]*width\s?=\s?["\'])[^"\']+(["\'])~i', '${1}580${2}', $eventDescLong);
$eventDescLong = preg_replace('~(<iframe[^>]*height\s?=\s?["\'])[^"\']+(["\'])~i', '${1}387${2}', $eventDescLong);

 

 

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.