In my PHP application I am displaying embedded videos from various sources - youtube, vimeo etc.
In my articles table I store the id of my embed videos:
Article.video_url = 'e5dkjwkdek'
Then in my video_providers table I store the embed code, but I want to dynamically add the video_url, so I store:
VideoProviders.embedcode = "https://www.youtube.com/embed/$article->video_url"
I then output the 'embedcode' variable in my template and I want to append the $article->video_url which is the unique id of the video.
It seems I need a variable within the variable, I tried:
VideoProviders.embedcode = "https://www.youtube.com/embed/{$article->video_url}"
and
VideoProviders.embedcode = "https://www.youtube.com/embed/$$article->video_url"
But it seems these are simply treated as literal strings. If anyone has any better suggestions as to how to achieve this I am all ears. In short I want the mark-up stored in one place so that is can be edited should the embed code of the provider change.
You cannot store strings with PHP interpolation symbols. Double-quoted strings are interpolated at runtime.
I'd say you have three options...
Store the various parts in your database and construct them in your query. For example (assuming MySQL)
SELECT
CONCAT(VideoProviders.embed_prefix, Article.video_url) AS embedcode,
-- etc
with VideoProviders.embed_prefix containing 'https://www.youtube.com/embed/', 'https://player.vimeo.com/video/', etc
Store the URL with placeholders for the video id and (again assuming MySQL) use the REPLACE function. For example
VideoProviders.embedcode = 'https://www.youtube.com/embed/{VIDEO_ID}'
and
SELECT
REPLACE(VideoProviders.embedcode, '{VIDEO_ID}', Article.video_url) AS embedcode,
-- etc
This would be the preferred solution if the video id doesn't always appear at the end of the URL.
Store a printf style pattern in embedcode, eg 'https://www.youtube.com/embed/%s' and put it together with PHP, eg
$embedcode = sprintf($row['embedcode'], $row['video_url');
I think you can just do this:
$video_id = 'e5dkjwkdek';
$provider_url = 'https://www.youtube.com/embed/';
$embed_code = $provider_url . $video_id;
Or something similar to this. You can fill the video_id and provider_url anyway you like. From a database or from some other source. This way should always work.
Related
Pulling in data from a Filemaker Pro database field and trying to convert the plain text data from the field into a clickable link to google maps via PHP.
My first attempt doesnt display anything when called:
$Venue = '';
$Venue is then echoed into a UL via
<?php echo $Venue; ?>
I'm relatively new to PHP so I'm sure there is a much more semantic way of marking this up? Possibly a regex and replace, returning a preg_replace? Which is what I've been using for plain text URLs and email addresses.
Anything helps, thanks so much.
Casey - not sure there's enough context here to help you? For instance, forget the link wrapping, does
<?php echo $record->getField( 'Auctions::AIS_Venue'); ?>
actually echo the field contents to a page?
Also, not sure you really want to use nl2br anyway, as you probably don't want a break in your url ;-)
OK, in which case there are a couple of things to try. First - can you get it to run in a webviewer inside FileMaker? I suspect not, and it may be down to an encoding/filtering issue.
There are two approaches you could use.
Create a new calc field in FileMaker that filters out punctuation and converts spaces to '+'s, something like:
Filter( Substitute( Lower( Auctions::AIS_Venue ) ; [ ¶ ; "+"] ; [" " ; "+"]; ["++" ; "+"] ); "abcdefghijklmnopqrstuvwxyz1234567890+" )
and then use that field as in your PHP code, or;
Do the same calculation on the fly in PHP, something like:
$Venue = '';
That should do the trick ? ;-)
I am making the dynamic website in PHP. While code is running good using pageid.
The url is now like www.google.com/pageid=1
Now, i want to change the url for 1 level pages as www.google.com/page1
for level 2 as www.google.com/page1/page2
for level 3 as www.google.com/page1/page2/page3
While unique address is stored in the my table as page1,page2,page3.
How can it be possible to change url at run time. Please give the examples and comments. So that it may helpful to understand.
Also i want to know if it is possible using the .htaccess file. If possible How .htaccess %{REQUESTED_FILE}% will reach out for my unique url's that are stored in the database.
$URL= "www.google.com/page1"
1) split the string with '/'
$data = explode("/",$URL);
$data[1] will have page1 value
2) replace the string page with ""
$pageId = str_replace("page","", $data[1]);
3) whatever is left is the page id
pageId has the value now.
Use that to query the database.
Another way is to use regex to extract the number from page1 and use it where-ever you want to use it.
Hi I am implementing a comment system . When showing my comments and articles in my view again , i want to use htmlspecialchars but i want my hypelinks can be clicked .
Example Comment :
My favourite web site is < www.facebook.com >.
Then i the backend i change this : to
My favourite web site is < www.facebook.com >
and save in the database .
now i am showing this comment in view , if i used echo htmlspecialchars($message) the message will be
My favourite web site is < www.facebook.com >
But i want my link to be a hyperlink , but other part should be using htmlspecailchars
I can check for the hypelink in the string and do some complex logic to do add htmlspecailchars only to other parts . I have two questions .
Is it good practice to add a hyperlink to the database or it should be created when viewing data ?
2.What is a better way to use htmlspecialchars for only strings other than hyperlinks .
like exclude only the <a> tags
in htmlentities function i saw optional character-set parameter A string that specifies which character-set to use . is there anything like exclude character set .
Thanks in advance
Your database should store raw data (sanitized for sure but not transformed). Thus the transformation link → link should happen just before view time, not before DB storage.
As for the why you should store raw data: Nothing tells you that one day you won't use your data in other things than HTML, thus the formatting would have to be cleaned up for this other use.
To sum up:
You should store raw (sanitized for SQL)
Retrieve raw
Sanitize HTML
Transform links
Display
I'm putting together an extremely simple ad server with php and mysql on a website. The site currently has 4 or 5 static pages with lots of dynamic content, so URLs look like this:
index.php?pid=1 or content.php?spec=2
What I'd like to do is add a field to my table of ads to keep track of that page(s) the ad is going to be displayed on.
Should I store URLs that have an ad as a list of comma separated values?
Once I retrieve this variable, what's the best way to separate the values into an array?
What's the best way to split a string so I can split the page name $_GET name and variable (as in 'index, pid, 1' or 'content, spec, 2' using the examples above.) ??
Additional Info:
As an example, doctors.php is structured something like this:
doctors.php Listing of Doctor Specialties
doctors.php?spec=# Listing of Doctors that have a particular
specialty
doctors.php?pid=# One specific Doctor's information
I have a few dozen specialties, and a few hundred doctors. I want to be able to place an ad on specific pages/URLs, say doctors.php?pid=7
But I also want to be able to place an ad based on, say, all of the doctors who have a specialty with the ID of 6. That could be 60+ pages, so it doesn't really make sense to have separate table rows. If I needed to change the link on the ad, I don't want to have to change it 60 times or search for it.
Don't store as a CSV.
Add separate database rows for each ad / URL combination.
Then retrieving content will be trivial.
Store URls one per line in simple file. Than you can use php function "file" to read this file as an array.
For url split use http://php.net/parse_url function
Here's what I think will work...
My ad table will have three variables, (a, b, c)
If I have an ad placed on doctors.php?spec=12, I'll store the following:
a = 'doctors';
b = 'spec';
c = '12';
If the ad was meant to display on ALL specialty pages, I'd store:
a = 'doctors';
b = 'spec';
c = NULL;
If something is NULL, it will simply indicate ALL of a set. It seems like an elegant solution, I'll post code if it works.
$url = "index.php?pid=1";
$pos = strpos($url, "?");
if ($pos != false) {
$pieces1 = explode("?", $url);
$pieces2 = explode("=", $pieces1[1]);
$array = array($pieces1[0], $pieces2[0], $pieces2[1]);
}else{
$array = array($url, '', '');
}
print_r($array);
You'll need to improve this code if you want it to work for multiple variables in your url for ex: index.php?v=hello&t=world
Also, i did not test this code. I just wrote it without checking the functions etc.
I have a site that offers a keyword search. The user can perform a search by either selecting from a set of pre-defined keywords displayed as hyperlinks or utilize a search form on the same page.
When the user searches for Russian Blue Cat, the following is added to the page URL:
If using the pre-defined hyperlink search term, then ?keywords=Russian%20Blue%20Cat is added to the URL as follows:
http://mydomain.com/index.php?keywords=Russian%20Blue%20Cat
If using the search form, then ?keywords=Russian+Blue+Cat is added to the URL as follows:
http://mydomain.com/index.php?keywords=russian+blue+cat
The following $_GET line of code is placed within two PHP files, the original index.php file that contain both the pre-defined hyperlink search terms and the search form and another PHP file called process.php that utilizes the keywords for another process.
if(empty($_GET['keywords'])){$keywords = '';} else {$keywords = $_GET['keywords'];}
The above $_GET line of code contained within the index.php file works properly and retrieves all three keywords. In this case the words Russian Blue Cat is retrieved.
The above $_GET line of code contained within the process.php file does not work properly and only retrieves the first of the three keywords. In this case only the word Russian is retrieved.
Is there a simple or proper way to fix this such that all keywords are retrieved properly?
Thank you in advance.
Check for the string '%20' and if present, explode by '%20'. Otherwise, check for the presence of a plus sign and explode by it instead.
This method is agnostic of the differing input format from the two sources entering the same script.
The caveat is that the string '%20' or the character '+' cannot occur inside a word in the other format or you will get unusual behavior.
$keyword = array();
if(stripos($keywords,"%20")) {
$keyword = explode("%20",$keywords);
}
else if(stripos($keywords,"+")) {
$keyword = explode("+",$keywords);
}
'$keyword' will then contain your keywords in an array.