Hi I am trying to insert my article title in database in the following format this-is-a-new-title for the input This is a new Title. For this I have written :
$title = $this->input->post('topic_title');
$topic_slug_title = url_title($title,'-',TRUE);
But the echo $topic_slug_title shows titles like this_is_a_new_title. Why the underscores are added wherein I have given hyphens ?
Ok I found the solution. Just leave the second parameter as default,add the third parameter. That is:
$title = $this->input->post('topic_title');
$topic_slug_title = url_title($title,TRUE,TRUE);
don't use the third parameter, use it like this
$title = $this->input->post('topic_title');
$topic_slug_title = strtolower(url_title($title));
don't use second and third parameter, write like this:
**$title = $this->input->post('topic_title');
$topic_slug_title = url_title($title);**
Related
I want to echo only the first word of a url parameter. For example: /?name=Mike%20Jackson. I want only "Mike". How is that done?
I can get the whole parameter.
<?php // show welcome back message if coming from the waiting list email
$name = '';
if (isset($_GET["name"]))
{
$fullname = $_GET["name"];
}
?>
I expect the first word only.
To break the string at a space, you would use the explode function. That separates the parameter and breaks it into an array, so to get the first word you just need to get the 0th index:
$fullname = explode(" ", $_GET["name"])[0];
If space in your URL is encoded as in your example, you just need to replace the space with the encoding:
$fullname = explode("%20", $_GET["name"])[0];
Edit: As pointed out in the comments, the first method should be the way to go because your method of getting the parameter automatically decodes the URL.
I'm doing a custom page for my WP website and for this I'm getting the content of an existing WP page. Getting the content is not a problem but I'm getting it like this :
https://youtu.be/abcdefghijkhttps://youtu.be/kjihgfedcbahttps://youtu.be/abcdefghijk
I'd like to transform it like this :
https://youtu.be/abcdefghijk
https://youtu.be/kjihgfedcba
https://youtu.be/abcdefghijk
I tried explode('/', $content) but it's not working as I want.
I don't know if I can use substr() in this case.
How can I do to separate each url properly or atleast separate each video's id ?
You can get all video id(s) as an array like this:
$str = "https://youtu.be/abcdefghijkhttps://youtu.be/kjihgfedcbahttps://youtu.be/abcdefghijk";
$videos = explode("https://youtu.be/",$str);
If you print_r($videos); you will see all the id in an array.
If you want these array values as full URLs, then do like this:
$videos = array_map(function($value) { return ' '.$value; }, $videos);
I'm currently hardcoding an include into my pages and its a lot of work everytime I want to create a new page. Currently, my url is like this:
http://example.com/folder/one-z-pagename.php?var1=one&var2=two
and in one-z-pagename.php I have an include that looks like this:
include("lander-a-pagename.php");
So what I want to do is instead of hardcoding the file into the page like above, I want to grab one-z-pagename.php without the ?var1=one&var2=two from the url, erase the first 6 characters which is one-z- and replace it with lander-a-.
How do I do something like this?
use parse_url and basename to get filename then str_replace
$url= parse_url('http://example.com/folder/one-z-pagename.php?var1=one&var2=two');
$file = basename($url['path']);
$newfile = str_replace('one-z-','lander-a-',$file);
echo $newfile;
output : lander-a-pagename.php
It can be achive by many method, have a look on below two methods:
Method 1:
$current_script_name = basename(__FILE__);
$include_script_name = 'lander-a-'.substr($current_script_name, 6);
Method 2:
$current_script_name = $_SERVER['SCRIPT_NAME'];
$current_script_name = end(explode('/', $current_script_name));
$include_script_name = 'lander-a-'.substr($current_script_name, 6);
//OR
$include_script_name = str_replace('one-z', 'lander-a', $current_script_name);
variable $include_script_name contains required filename which you want to include.
Hope this will help.
below is my code, i need to write URL value in file with define path. but it output as URL and not replace with his define value, $string variable value cone from database.
<?php
define("URL","http://example.com/mail.php");
$string = 'Page here : {{URL}}'; // string come from database using query //
//$string = str_replace("{{","'.",$string);
//$string = str_replace("}}",".'",$string);
$f = fopen("d:\abc1.txt",'w');
$wr = fwrite($f,$string);
fclose($f);
?>
output in file was : Page here : {{URL}}
but i need something like this : Page here : {{http://example.com/mail.php}}
i also try using comment line to replace string and get out URL into string.
Well, if I get it right, you have a dynamic string from DB, which somewhere contents this: '{{URL}}'. So just do simple replace like this:
define("URL","http://example.com/mail.php");
$string = str_replace('{{URL}}', '{{' . URL . '}}', $string);
This replaces URL text using URL value of that constant
like this?
define("URL","http://example.com/mail.php");
$string = 'Page here : {{' . URL . '}}';
Use your syntax like this :
$string = "Page here : {{". URL ."}}"; // string come from database using query //
As Mikec007 mentioned str_replace() will work for you in this case.
I am trying to grab the second argument of a URL such as this:
http://website.com/?p=2?id=ass92jdskdj92
I just want the id portion of the address. I use the following code to grab the first portion of the address:
$p = $_GET[p];
$remove = strrchr($p, '?');
$p = str_replace($remove, "", $p);
Any hints on how to get the second portion?
Arguments in links are started by ? and divided by &.
That means your link should look like this:
http://website.com/?p=2&id=ass92jdskdj92
And you get them by:
$p = $_GET['p'];
$id = $_GET['id'];
First change the URL
only first argument start with ? and rest of all is append by &
u should try with
echo parse_url($url, PHP_URL_PATH);
Reference:
parse url