英文:
replace VARIABLE in $url string
问题
我有一个PHP代码,它从Wikipedia中打印文章到我的WordPress文章中。我的问题是如何替换$url字符串中的VARIABLE。
让我解释一下我的情况。
VARIABLE 是WordPress文章的标题,必须插入到$url字符串中。
如果标题只有一个单词,只需将其替换VARIABLE在$url字符串中。
如果标题有2个单词,我需要将单词之间的空格替换为%20。
解决这个问题的代码如下:
global $post;
$title = str_replace([" "], ["%20"], $post->post_title);
print $title;
这是我拥有的主要PHP代码。那么正确的方法是什么:
$url = "http://en.wikipedia.org/w/api.php?action=query&prop=extracts|info&titles=$title&format=json&explaintext&redirects&inprop=url&indexpageids";
请注意,您可以直接在$url字符串中嵌入$title变量,不需要再使用VARIABLE。
英文:
I have a PHP code, which prints article from Wikipedia into my wordpress article. My problem is to replace VARIABLE in $url string
Let me explain my scenario.
VARIABLE is: post title of wordpress which have to be inserted in $url.
If single word in title, just insert it replacing VARIABLE in string in $url
if 2 words I need to replace the space (period) between words to %20
And the code, which solves it:
global $post;
$title = str_replace([" "], ["%20"], $post->post_title);
print $title;
This is the main php code I have. So what is the right way to get
<?php
$url =
"http://en.wikipedia.org/w/api.php?action=query&prop=extracts|info&titles=**VARIABLE**&format=json&explaintext&redirects&inprop=url&indexpageids";
$json = file_get_contents($url);
$data = json_decode($json);
$pageid = $data->query->pageids[0];
$title = $data->query->pages->$pageid->title;
$string = $data->query->pages->$pageid->extract;
$getarticle = str_replace(
["==", "Biography", "References"],
["<br> <br>", "<b>Biography</b>", " "],
$string
);
print $getarticle;
?>
$url = 'http://en.wikipedia.org/w/api.php?action=query&prop=extracts|info&titles=$title&format=json&explaintext&redirects&inprop=url&indexpageids';
答案1
得分: 1
- 使用
parse_url
函数解析URL。 - 从上述函数的输出中,使用
query
键获取查询参数。 - 基于
&
进行拆分,并将您的$title
变量添加到titles
键。 - 将查询字符串合并回去,重新构建URL。
$parsed_url = parse_url($url =
"http://en.wikipedia.org/w/api.php?action=query&prop=extracts|info&titles=**VARIABLE**&format=json&explaintext&redirects&inprop=url&indexpageids");
$params = [];
parse_str($parsed_url['query'], $params);
$title = 'Some example'; // $data->query->pages->$pageid->title;
$params['titles'] = $title;
$parsed_url['query'] = http_build_query($params);
$url = $parsed_url['scheme'] . '://' . $parsed_url['host'] . $parsed_url['path']. '?' . $parsed_url['query'];
echo $url;
英文:
- Parse the URL using
parse_url
function. - Get the query params using
query
key from the output of the above function. - Explode based on
&
and add your$title
variable totitles
key. - Implode the query string back and make your URL again.
Snippet:
<?php
$parsed_url = parse_url($url =
"http://en.wikipedia.org/w/api.php?action=query&prop=extracts|info&titles=**VARIABLE**&format=json&explaintext&redirects&inprop=url&indexpageids");
$params = [];
parse_str($parsed_url['query'], $params);
$title = 'Some example';// $data->query->pages->$pageid->title;
$params['titles'] = $title;
$parsed_url['query'] = http_build_query($params);
$url = $parsed_url['scheme'] . '://' . $parsed_url['host'] . $parsed_url['path']. '?' . $parsed_url['query'];
echo $url;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论