Bruno
Bruno

Reputation: 9017

Concatenation of HTML string using PHP or Javascript

I need to do a HTML string concat (using PHP or Javascript) and I don't know how to do it.

What I'd like

<src="http://maps.google"+$city+"rest_of_the_link">

(I've already tried the previous code but it doesn't do the trick)

Upvotes: 2

Views: 24894

Answers (7)

Thomas
Thomas

Reputation: 5856

<src="maps.google<?php echo $city ?>rest_of_the_link">

Assuming you're using PHP and this is in a .php file.

Upvotes: 0

Uri
Uri

Reputation: 26966

I think what you're looking for is templating.

You can load the HTML as a template, and then plant values in specific locations. If you load it from the server side you can use django templates (I use django and my backend is in python), and if you're doing it from the client size, through javascript, you can use jquery templates or the jqote plugin for jquery which is apparently faster than the native jquery solution.

For example, If you're using django, your html would look something like this:

<src="http://maps.google{{city}}rest_of_the_link">

And when you load it using the server, you pass a dictionary object that has a value for the key 'city'.

I'm not sure what is the best method for php templates, but this is something you can probably find easily when you know what you're searching for.

Upvotes: 1

Your Common Sense
Your Common Sense

Reputation: 157839

Using PHP:

<src="http://maps.google<?=$city?>rest_of_the_link">

Upvotes: 2

Priyashil
Priyashil

Reputation: 1

Html is not a programming language, you can achieve it by using JavaScript DOM or JQuery. Just access the the anchor object, manipulate and assign its attribute.

Upvotes: 0

Bas Slagter
Bas Slagter

Reputation: 9929

You need a serverside language like PHP or ASP to do this. You can also build the url dynamically using Javascript (or a combination of one of these options) but your question does not make clear where the value of the variable must come from.

Upvotes: 0

Madara&#39;s Ghost
Madara&#39;s Ghost

Reputation: 174957

You cannot do that with HTML alone. HTML is not a programming language, it's a markup language (hence the HyperText Markup Language), for that reason, no variables (or control structures, or anything of that sort) is in it.

What you need to do, is to supply yourself with a different language to help generate the HTML.

For example, in PHP you'd do something like so:

<?php
    $city = "London"; //Set the variable
    echo "<src=\"http://maps.google".$city."rest_of_the_link\">"; //Echo result concatenating $city inside.
?>

Upvotes: 3

Quentin
Quentin

Reputation: 943207

You can't. HTML doesn't have strings … or variables. It is a markup language, not a programming language.

You can generate HTML from another language (which could be a program that is executed automatically by a webserver to generate the content for a URL), and you can use DOM APIs (again from another language) to manipulate a DOM generated from an HTML document.

Upvotes: 4

Related Questions