Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6542179
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:11:40+00:00 2026-05-25T11:11:40+00:00

How can I get only the text of the node <p> which has other

  • 0

How can I get only the text of the node <p> which has other tags in it like:

<p>hello my website is <a href="www.website.com">click here</a> <b>test</b></p>

I only want “hello my website is“

This is what I tried:

begin
  node = html_doc.css('p')
  node.each do |node|
    node.children.remove
  end
  return (node.nil?) ? ''  : node.text
rescue
  return ''
end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T11:11:40+00:00Added an answer on May 25, 2026 at 11:11 am

    Update 2: all right, well you are removing all children with node.children.remove, including the text nodes, a proposed solution might look like:

    # 1. select all <p> nodes
    doc.css('p').
      # 2. map children, and flatten
      map { |node| node.children }.flatten.
      # 3. select text nodes only
      select { |node| node.text? }.
      # 4. get text and join
      map { |node| node.text }.join(' ').strip
    

    This sample returns “hello my website is”, but note that doc.css('p') als finds <p> tags within <p> tags.

    Update: sorry, misread your question, you only want “hello my website is”, see solution above, original answer:

    Not directly with nokogiri, but the sanitize gem might be an option: https://github.com/rgrove/sanitize/

    Sanitize.clean(html, {}) # => " hello my website is click here test "
    

    FYI, it uses nokogiri internally.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

can we get the values of the radio buttons , only those which has
example: at this domain http://www.example.com/234234/go.html is only one iframe-code how can i get the
How can I get only text from twitter statuses/user_timeline and match it with php
<a href=#>Domain name<span class=value>2</span></a> I would like to retrieve only the text Domain name.
How can I get only unique departments from the below example? Dept Id Created
How can I get and set the 'read-only' property of an edit box?
How can I get superscript done, only in CSS? I have a stylesheet where
In nodeJS, we can get POST data by this: <input type=text name=doc[a]/> var doc
Can get all triples with value null in specific field? All people with date_of_birth
I can get easily see what projects and dlls a single project references from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.