PHP

How to extract a single column values from CSV file in PHP

How to extract a single column values from CSV file in PHP

CSV, PHP
Importing data from a CSV file with PHP may mean you need to store or print just the results of a single and specific column of the table. To make an example, in the following case, I need to extract data from "color" column: fruit, color, price tomatoes, red, 0.08 oranges, orange, 0.12 bananas, yellow, 0.12 To extract the single color column from the table, fgetcsv PHP function can be used. This function returns each line values from a CSV table. In the loop, changing the value of $c variable, will output results of each line starting from $c variable value. Now remove $num variable and add  $c=0; $c < 1 (if just first column is wanted) [or $c=1; $c < 2 for second column or also $c=0; $c < 2 for first and second columns and so on] prevents to print all the following
How to store WordPress Post Content in to a PHP variable

How to store WordPress Post Content in to a PHP variable

PHP, Wordpress
If you are developing something with Wordpress you may need to create a variable in which to insert the content of a post. Something like: $content = ..post content.. Classic the_content() function just prints the content of the article but does not allow to save it as a variable, unless you use content buffer. On the other hand  the content can be easily stored into a variable using get_the_content() function, even if if it does not return the content in the same way the_content() does it, since text is unformatted. You can call get_the_content() function even outside the loop using the global variable $post: global $post; $content = $post->post_content; In so doing post content will be saved within the $content variable and will then be available to you when

Str_replace that En Dash (or minus sign)! How to fix the problem when it’s not working

HTML, PHP, Wordpress
Building my first theme on Wordpress I had been a very long time in to understand how to use str_replace function to change title of a page to remove all the words following another. This was approximately the string to change: i had name and surname of a specific person (John Doe) and what i wanted to do was to remove all the rest of the phrase. Page Title:  John Doe Contact Page – Sitename String i wanted to get as result after str_replace:  John Doe So practically i had to get just "John Doe" from that page title. The easiest way i knew was to use str_replace function in PHP in the following way: Getting the Wordpress Title as a variable: <?php $string = get_the_title(); $replace = " Contact Page - Sitename"; $replacewith   = " "; ?> Then using str_re