Retrieve only the category ID from URL - php

I have my URL structure as www.site.com/listings/232-real-estate where 232 is the category ID and real-estate as my category name.
I want to retrieve only the numeric ID, to be used to extract data from the database using the category ID. How do I do that?

You can use something as simple as:
<?php
preg_match_all('#(\d+)#', $url, $matches);
$catid = $matches[0];
?>
But this does not have adequate checks in place. Ideally, you want to split on basis of /:
<?php
$elements = explode('/',$url);
$category = $elements[2];
$subelements = explode('-',$category);
$categoryid = intval($subelements[0]);
?>

You can use preg_replace:
id = preg_replace('/^.*\/([0-9]*)-[^\/]*$/','$1')
This assumes that the last / in the url is just before the category ID.

If 232-real-estate is stored in the variable $var you can get the ID using:
$id = strtok($var, '-');
Incidentally, it is then trivial to get the name:
$name = strtok(''); // returns: estate-agent

Related

url_title codeigniter showing underscores instead of hyphens

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);**

Get specific part of a URL value

I used a code to bulk import the envato items,
for example the envato items url is like this :
http://themeforest.net/item/avada-responsive-multipurpose-theme/2833226
How to get only the part "2833226" of this URL with PHP?
I use wordpress and have used custom fields to insert envato item links
for example a custom field (afflink) with value :
http://themeforest.net/item/avada-responsive-multipurpose-theme/2833226
and this code to call the custom field value in the theme
$afflink = get_post_meta($post->ID, "afflink", false);
How to get only the item number?
Use explode to split using / caracter and then get the last element of array:
<?php
$url='http://themeforest.net/item/avada-responsive-multipurpose-theme/2833226';
$y=explode('/',$url);
$affiliateID = end($y);
?>
$pattern = "/\d+$/";
$input = "http://themeforest.net/item/avada-responsive-multipurpose-theme/2833226";
preg_match($pattern, $input, $matches);
//Your ID
$post_id = $matches[0];
Use this :
value = window.location.href.substring(window.location.href.lastIndexOf('/') + 1);
$url = 'http://themeforest.net/item/avada-responsive-multipurpose-theme/2833226?r=1';
$urlParts = parse_url($url);
$path = $urlParts['path'];
$pathParts = explode('/',$path);
$item_id = end($pathParts);
The above code will make sure to avoid query string and read only Uri

GET_CONTENT name from txt file

i have this list on name.txt file :
"name1":"Robert"
"name2":"George"
"name3":"Flophin"
"name4":"Fred"
in a web page i need a php code that takes only the name of the person by the name 1 2 3 4 id.
I've use this in test.php?id=name2
$Text=file_get_contents("./name.txt");
if(isset($_GET["id"])){
$id = $_GET["id"];
$regex = "/".$id."=\'([^\']+)\'/";
preg_match_all($regex,$Text,$Match);
$fid=$Match[1][0];
echo $fid;
} else {
echo "";
}
The result should be George ,
how do i change this to work??
Mabe is another way to do this more simply?
$file=file('name.txt');
$id = $_GET["id"];
$result=explode(':',$file[$id-1]);
echo $result[1];
Edit: $result[1] if you want just name.
Heres an ugly solution to your problem, which you can loop trough.
And Here's a reference to the explode function.
<?php
$text = '"name1":"Robert"
"name2":"George"
"name3":"Flophin"
"name4":"Fred"';
$x = explode("\n", $text);
$x = explode(':', $x[1]);
echo $x[1];
Load the text file into an array; see Text Files and Arrays in PHP as an example.
Once the array is loaded you can reference the array value directly, e.g. $fid = $myArray['name' . $id]. Please refer to PHP how to get value from array if key is in a variable as an example.

Parsing a single XML section using a query string with PHP

I'm trying to retrieve a single section from an XML file using PHP.
Here's the code I'm using:
<?php
$articles = simplexml_load_file('articles.xml');
foreach ($articles as $articlecontent)
{
$title = $articlecontent->title;
$content = $articlecontent->content;
$author = $articlecontent->author;
$date = $articlecontent['date'];
echo "<h1>",$title,"</h1>\n",
"<p><i>",$date,"</i></p>\n",
"<p>",$content,"</p>\n",
"<p>",$author,"</p>\n",
"<p><time>",$date,"</time></p>\n"
;
}
?>
Which shows all the sections in the XML file, but how can I parse just one result but do so using a query string?
Example: articles.php?title=section-one will retrieve the XML section with the title "section-one"
You get the title from the query string by getting it from PHP's superglobal $_GET array, like this:
$title = false;
if (isset($_GET['title']))
{
$title = $_GET['title'];
}
and then the simplest way would be to compare it to the title from the XML file in your foreach loop.
Way to get title from url is using $_GET array, then to get required article by title i think you could use xpath which won't require foreach loop
$title = $_GET['title'];
// there should go some sanitizing
$articles = simplexml_load_file('articles.xml');
//search for article by title
//assuming that xml root tag is articles and article tag is article and searching by attribute named title
$foundArticles = $articles->xpath('articles/article[#title='.$title.']');
//query depends on xml structure
// now you have array of matched articles with matching title from url
foreach($foundArticles as $singleArticle)
{
//do printing here
}
This code is not tested, but principle should work.
To receive Query String Variable (in our case 'title') from URL you can use $_REQUEST also. Little bit corrected code (#user1597483) with some more advanced updates.
$articles = simplexml_load_file('articles.xml');
$title = "";
if(isset($_REQUEST['title'])) //receiving Query String from URL
$title = $_REQUEST['title'];
if(count($articles)):
//Fixed search means that will one return article if 'section-one' in title
//$result = $articles->xpath("//article[title='Section-one']");
//Match All Search means that will return all articles that contains 'section-one' in title
$result = $articles->xpath("//article[contains(translate(title, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'),'".strtolower($title)."')]");
foreach ($result as $articleItem):
$title=$articleItem->title;
$content=$articleItem->content;
$author=$articleItem->author;
$date=$articleItem->date;
echo "<h1>",$title,"</h1>\n";
echo "<p><i>",$date,"</i></p>\n";
echo "<p>",$content,"</p>\n";
echo "<p>",$author,"</p>\n";
echo "<p><time>",$date,"</time></p>\n";
endforeach;
endif;
In the code above I have specified two types of filter through which you can get result with Specific Article by exact match or wild card type matching by contains function. And I have also used translate function which will convert value of title element to lowercase and also converted value of Query String Variable to lowercase. So that regardless of Upper/Lower case, condition will be checking with lowercase and if condition matched then resultant articles will be returned.
You can achieve that by inserting a condition into the foreach loop testing if the title-filter is in action and does not match. If so, skip that entry by using the continue keyword:
$titleFilter = trim(isset($_GET['title']) ? $_GET['title'] : '');
...
foreach ($articles as $articlecontent)
{
$title = $articlecontent->title;
if (strlen($titleFilter) and $title !== $titleFilter) {
continue;
}
...

How to get the id in the URI when using CodeIgniter

I'm working on a CodeIgniter project and trying to get the id of this URI:
http://example.com/myDomain/index.php/public/calendar/gestionEvent?id=c2pnumqnqr0dg9tgmaklho5280#google.com
I want the controller to delete or update the event using this ID.
I tried $this->uri->segment(4)but it doesn't seem to work and doesn't show anything.
try this :
$id = $this->input->get_post('id');
echo $id;
in cotroller
$id = $this->input->get('id');
$data['id'] = $id ;
you can not use uri segment here
ex:-http://example.com/index.php/news/local/metro/crime_is_up
here you can use uri segments
The segment numbers would be this:
1.news
2.local
3.metro
http://codeigniter.com/user_guide/libraries/uri.html
UPDATE
if you want to get only the id with out #google.com you can simply use explode()
$id_string = $this->input->get('id');
$id_string_exp = explode("#", $id_string);
$id = $id_string_exp[0];
Then use
$id = $this->input->get('id');
$this->uri->segment(4) will work only if the url is in the form of segments like
http://example.com/myDomain/index.php/public/calendar/gestionEvent/c2pnumqnqr0dg9tgmaklho5280#google.com
Then you will get id using "URI segments".Thanku.i hope this helps you friend

Categories