shahryarjb

shahryarjb

How to get the string concerned with html helping

Hello, I want to count 3 </p> html tag form my string and take them like this, it should be noted I need all of these sting and html tags:

my site contents:

<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit</p> <!--  1 -->
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit</p> <!--  2 -->
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit</p> <!-- 3 -->
<!-- I need the three items above  -->
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit</p>

how do I do cut that? I need those for creating read more in my site.

I have seen String.slice(0..29) , but it hasn’t work for me.

thanks

Marked As Solved

kokolegorille

kokolegorille

Once You have Floki…

iex(1)> html = "<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit</p> <!--  1 -->
...(1)> <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit</p> <!--  2 -->
...(1)> <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit</p> <!-- 3 -->
...(1)> <!-- I need the three items above  -->
...(1)> <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit</p>"
iex(2)> output = Floki.find(html, "p")         
[
  {"p", [], ["Lorem ipsum dolor sit amet, consectetuer adipiscing elit"]},
  {"p", [], ["Lorem ipsum dolor sit amet, consectetuer adipiscing elit"]},
  {"p", [], ["Lorem ipsum dolor sit amet, consectetuer adipiscing elit"]},
  {"p", [], ["Lorem ipsum dolor sit amet, consectetuer adipiscing elit"]}
]

and then, it’s just a list

iex(3)> output |> Enum.take(3)
[
  {"p", [], ["Lorem ipsum dolor sit amet, consectetuer adipiscing elit"]},
  {"p", [], ["Lorem ipsum dolor sit amet, consectetuer adipiscing elit"]},
  {"p", [], ["Lorem ipsum dolor sit amet, consectetuer adipiscing elit"]}
]

PS: To transform data back to html, You might use a final pipe like this

|> Floki.raw_html

Also Liked

idi527

idi527

:wave:

To avoid cutting <p> tags half-way through, parse the html (if it hasn’t been parsed already) with something like floki, take three <p> nodes from it and then possibly render it back into a string.

kokolegorille

kokolegorille

Glad You could solve this.

I would like to mention, as a personal opinion, that I don’t find practical to mix styles with elements. For this, I would prefer to use CSS. You don’t even need to style each element, I would use a wrapper class (eg: long, short).

.long p { whatever style here }
.long span {}
.short p {}
.short span {}

etc.

Thus You would not need to change styles at all, it would depend on higher CSS context.

Of course that does not apply if You are not allowed to change layout.

PS: If You use SASS, it is easy to define hierarchy in your styles.

Where Next?

Popular in Questions Top

Tee
can someone please explain to me how Enum.reduce works with maps
New
LegitStack
I’m hoping you guys can give me some general advice and perhaps code examples if you’re feeling up to it. I’m very interested in Elixir,...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
Kagamiiiii
Student &amp; New to elixir. Nice language. I want to convert a english character, e.g. “a”, which is stored in a variable, to it’s asci...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
gonzofish
I’m currently trying to understand how to join three tables using Ecto. All the examples I’ve seen use 2, so maybe I’m just missing somet...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

Other popular topics Top

JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1140 51847 244
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New

We're in Beta

About us Mission Statement