开发者

Concatenation of HTML string using PHP or Javascript

开发者 https://www.devze.com 2023-04-01 06:40 出处:网络
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

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_o开发者_高级运维f_the_link">

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


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.


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.
?>


Using PHP:

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


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.


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.


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.


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

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号