How can I separate a string of urls and print each link? - php

How can I separate each link from my string:
$field = "www.link1.com
www.link2.com";
And output them like this (expected output):
link1 title
www.link1.com
link2 title
www.link2.com
My current code looks like this:
<?php
$field =
"www.link1.com
www.link2.com";
if ($field == "link1");
{
$output="link1 title</br>".$field ;
}
echo $output;
?>
But it only outputs this (current output):
link1 title
www.link1.com www.link2.com
So how can I change/modify my code to get the urls separated and print them like shown above?

This should work for you:
Here I first explode() your string into an array, so that we have each url as array element.
Then we just loop through each link and print them. We also grab the name between www. and the next dot with preg_replace().
$arr = array_map("trim", explode(PHP_EOL, $field));
foreach($arr as $v) {
echo $v . " title<br>";
echo preg_replace("/^www\.([^\.]*)(.*?)$/", "$1", $v) . "<br><br>";
}

Related

separate each words in php

I had an value in database like "demo text" . I want to display this content from the db in the view page as
Html code that i am using is like this <h2>Demo<span>Text</span></h2> , is there any solution for seperate each words and use one for h2 and other for span. I am using php codeigniter for the project , I don't know that whether the way i explained my problem is correct or not .
Yes you can so it with explode
if you have stored at least 2 words with space. try following
$demo ="demo text";
$arr = explode(" ",$demo);
$str = "<h2>".$arr[0]."<span>".$arr[1]."</span></h2>";
echo $str;
DEMO
EDIT
If you have more words and want to split first word only you can pass limit parameter in explode
$demo ="Pligrimage to Marian Shrines";
$arr = explode(" ",$demo,2);
$str = "<h2>".$arr[0]."<span>".$arr[1]."</span></h2>";
echo $str;
DEMO
I think you are looking for something like this!
$your_string = "Hello Houston! We have a problem!";
$my_array = explode(" ",trim($your_string));
$output = "<h2>";
foreach($my_array as $a_word){
if ($a_word === reset($my_array))
$output .= $a_word;
else
$output .= " <span>". $a_word . "</span>";
}
$output .= "</h2>";
print $output;

array unique not working fine on a tag string

I have been battling on how to display distinct tag that I stored in database. In my database I have saved some tags, when I tried displaying it back I used array unique but it didn't work. see below example database
item | tag
---------|---------------
code 1 | html,php,css
code 2 | jquery,xml,js
code 3 | php,python,xhtml
code 4 | css
Now when i select tag from my table i will get below
<?php
$tags = 'html,php,css
jquery,xml,js
php,python,xhtml
css';
//Here i
$string = explode(',', $tags);
foreach($string as $linetag){
//echo $linetag;
$result = array_unique($linetag);
echo $resul;
}
?>
But the above code is not working. I want to display unique tags and remove duplicates like below
html,
php,
css
jquery,
xml,
js
python,
xhtml
Try this for href anchor tag
$tags = 'html,php,css,jquery,xml,js,php,python,xhtml,css';
$string = explode(',', $tags);
$result = array_unique($string);
foreach($result as $d){
echo ' '."<a href=''> $d</a>";
}
$tags = 'html,php,css,jquery,xml,js,php,python,xhtml,css'; //string
$string = explode(',', $tags); //convert string into array by using php standard function
echo "<pre>"; print_r($string); // print array
$string1 = array_unique($string); //Removes duplicate values from an array
echo "<pre>"; print_r($string1); //print unique array
// if you want string again of this array use implode function
$unique_tags = implode(',', $string1);
echo $unique_tags;

how to explode and exploded string

I've a string of text like this:
Intro Title ### Some description ### a link \\\
Intro Title Two ### Description Two ### link 2 \\\
And so ... can be infinite
I use explode to be able to access and the different parts of the string.
$test = explode('###', $string);
echo $test[0]; // outputs: Intro Title
echo $test[1]; // outputs: Some description
and till here it's working fine. But I need to be able to access as well the second part the same way
echo $test[0]; // to output: Intro Title Two
I've tried with a foreach, but it seems to work
foreach ($string as $key) {
$second = explode('\\\', $key);
}
I can't figure out how to do it.
$string ="Intro Title ### Some description ### a link \\\
Intro Title Two ### Description Two ### link 2 \\\
And so ... can be infinite";
$firstExplode = explode('###', $string);
foreach ($firstExplode as $key) {
$secondExplode = explode("\\\\", $key);
var_dump($secondExplode);
}
Note here I use four backslashes instead of just the 3 due to escaping issues.
To answer you question
You can explode on the new-lines first and then in a foreach loop explode on your delimitter. Then you wouldn't even need the back slashes at the end of each line anymore.
<?php
$i = 0;
$lines = explode("\n", $string);
foreach($lines as $line) {
$data[$i] = explode('###', $line);
$i++;
}
To improve your code:
Unless you really depend on this custom file format I would recommend using a standard format like xml, yml or json.
The simplest approach woulr probably be json:
<?php
$string <<<EOT
[
{ "title": "Intro Title", "Description": "Some description", "link": "a link" },
{ "title": "Intro Title 2", "Description": "Some other description", "link": "a second link" }
]
EOT;
$data = json_decode($string, true);
print_r($data);
If it is one string :
$string ="Intro Title ### Some description ### a link \\\
Intro Title Two ### Description Two ### link 2 \\\
And so ... can be infinite";
$string_parts = explode("\\\",$string);
foreach($string_parts as $key=>$val){
$temp = explode('###', $val);
echo $temp[0]; // outputs: Intro Title
echo $temp[1]; // outputs: Some description
}

How to get the first paragraph bold in the php?

I am entering text in the database in two paragraphs
first paragraph
second paragraph
I am storing them in my database and when I am displaying them on the frontend using nl2br it is getting displayed perfectly.
I want the my first paragraph to be bold and the second paragraph should be normal.
I tried using strpos to find the location of the <br> tag after nl2br to chop off the first paragraph but I am not succeeding.
the failed code is
echo strpos(nl2br($row['article']), "<br>");
but i am not getting the position of the <br> tag
I got the correct answer from eddie, he deleted it but i am updating the answer here
$str='first paragraph
second paragraph';
foreach(explode("\n",) as $key => $val) {
if($key == 0){
echo'<b>';
}
echo $val;
echo'<br>';
if($key == 0){
echo '</b>';
}
}
Don’t use nl2br for the type of results you are looking for. Just split the string into an array using a regex rule with preg_split and then act on the first item in the array. Here is test code:
// Set the test data.
$test_data = <<<EOT
first paragraph
second paragraph
EOT;
// Split the test data into an array of lines.
$line_array = preg_split('/(\r?\n){1,2}/', $test_data);
// Roll through the line array & act on the first line.
$final_text = '';
foreach ($line_array as $line_key => $line_value) {
if ($line_key == 0) {
$line_value = "<b>" . $line_value . "</b>";
}
$final_text .= $line_value . "<br />\n";
}
// Dump the line array for debugging.
echo '<pre>';
print_r($line_array);
echo '</pre>';
// Echo the final text.
echo '<pre>';
echo htmlentities($final_text);
echo '</pre>';
die();
The output from the dump of the line array would be this:
Array
(
[0] => first paragraph
[1] => second paragraph
)
And the test output using htmlentities to show what was done HTML-wise:
<b>first paragraph</b><br />
second paragraph<br />
Try:
$first_line = explode(PHP_EOL, $str)[0];
$new_str = str_replace($first_line,'<b>'.$first_line.'</b>',nl2br($str));

Highlighting extracted keywords inside extracted description

Okay suppose we would like to get title,keywords and description of website so i'm going to use the following function
<?PHP
function getInfo($URL)
{
$getInfo= get_meta_tags($URL);
return $getInfo;
}
$URL = "http://www.my_site.com"; // URL
// Applying the function
$_getInfo = getInfo($URL);
// Print the results.
echo $_getInfo ["keywords"]."<br>"; // gives keywords
echo $_getInfo ["description"]."<br>"; // gives description
?>
Yet,everything if fine but suppose the results as following
Keywords
php,profitable,share
Description
Advanced profitable and featured script to share
As in this example we've some keywords found in description profitable and share
The question is how to highlight keywords that only found in description!!
I will add the following css
<style>
.highlight{background: #CEDAEB;}
.highlight_important{background: #F8DCB8;}
</style>
and will add this function to alter between two different colors just like in css code
<?PHP
function hightlight($str, $keywords = '')
{
$keywords = preg_replace('/\s\s+/', ' ', strip_tags(trim($keywords))); // filter
$style = 'highlight';
$style_i = 'highlight_important';
$var = '';
foreach (explode(' ', $keywords) as $keyword) {
$replacement = "<span class='".$style."'>".$keyword."</span>";
$var .= $replacement." ";
$str = str_ireplace($keyword, $replacement, $str);
}
$str = str_ireplace(rtrim($var), "<span class='".$style_i."'>".$keywords."</span>", $str);
return $str;
}
?>
Now applying both (Not working)
$string = hightlight($_getInfo["description"], $_getInfo ["keywords"]);
echo $string;
Why not working cause it define $_getInfo ["keywords"] as one word php,profitable,share
which indeed not found in description in that shape.
so how can i apply it by using explode or foreach (i guess) so the out put be like this :
I wonder if there was another way to do it if mine looks not good way. ~ Thanks
Since your keywords are in list format you need to:
foreach(explode(',', $keywords) as $keyword)

Categories